2013-03-11 62 views
0

我是新来创建Web服务,我不明白如何访问我的Web服务。如何通过URI引用WCF Web服务/如何通过URI将WCF服务POST?

我想要做的是创建一个WCF webservice,读取发布到它的JSON数据并将其反序列化,然后执行一些操作。

我已经创建了一个非常简单的WCF服务,其中暴露了两个方法并创建了一个uri端点。虽然当我去到我的uri时,我什么也没得到。

我应该可以导航到'http:// localhost:8000/asd/EchoWithGet?s = Hello,world!'在我的浏览器中,该方法应返回“你说”+ s。当我导航到服务运行时,我什么也得不到。

我的问题是如何界面我的程序?我是否也可以通过HTML表单发布我的服务,然后打开IO读取器?

在此先感谢您的帮助。

以下是我的代码。

namespace WcfService1 
{ 
[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    [WebGet] 
    string EchoWithGet(string s); 

    [OperationContract] 
    [WebInvoke] 
    string EchoWithPost(string s); 
} 

public class Service1 : IService1 
{ 
    public string EchoWithGet(string s) 
    { 
     return "You said " + s; 
    } 
    public string EchoWithPost(string s) 
    { 
     return "You said " + s; 
    } 
} 

class program 
{ 
    static void Main(string[] args) 
    { 
     WebServiceHost host = new WebServiceHost(typeof(Service1), new Uri("http://localhost:8000/asd/")); 
     ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService1), new WebHttpBinding(), ""); 

     /* 
     ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>(); 
     sdb.HttpHelpPageEnabled = false; 
     */ 

     host.Open(); 

     Console.WriteLine("Service is running"); 
     Console.WriteLine("Press enter to quit..."); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 

在此先感谢您的帮助

更新,我想我的问题,从我的配置文件造成的。我需要将哪些信息添加到配置文件中才能够通过浏览器使用我的Web服务?

<?xml version="1.0"?> 
<configuration> 
<system.web> 
<compilation debug="true" targetFramework="4.0" /> 
</system.web> 
<system.serviceModel> 
<behaviors> 
    <serviceBehaviors> 
    <behavior> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="false"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 
<system.webServer> 
<modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 
</configuration> 

回答

0

您可以使用下面的代码修改 并再次检查。

<system.serviceModel> 
    <services>  
    <service behaviorConfiguration="WcfService1.Service1Behavior" 
    name="WcfService1.Service1"> 
    <endpoint address="" behaviorConfiguration="JSONEndpointBehavior" 
     binding="webHttpBinding" contract="WcfService1.IService1" /> 
    <endpoint address="ws" binding="wsHttpBinding" contract="WcfService1.IService1"> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />  
    </service> 
</services> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="JSONEndpointBehavior"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior name="WcfService1.Service1Behavior"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
    </behavior>   
    </serviceBehaviors> 
</behaviors> 

和修改与下面码

[WebInvoke(Method = "GET", UriTemplate = "/EchoWithPost/{s}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 

串EchoWithPost(字符串s)的webvoke;

+0

可以通过删除如下服务标签<端点地址=“WS”绑定=“的wsHttpBinding”合同=“WcfService1.IService1内部端点尝试“><端点地址=” MEX的帮助”绑定=‘mexHttpBinding’合同=‘IMetadataExchange接口’/> – Parthi 2013-03-12 10:26:47

+0

嘿感谢球员,但遗憾的是没有这些解决方案解决我的问题。我已经正确创建了我的URI并正确创建了端点?所以我应该可以在本地访问我的服务?我可以使用WCF客户端,它工作正常,我只想通过浏览器访问它,并最终通过post方法访问它。 我是否正确创建了我的端点?谢谢。 – recneps 2013-03-12 19:42:59

+0

也原谅我不知道我在做什么,但为什么你添加'Get'动词到webInvoke属性?我认为webGet支持Get,webInvoke用于POST,PUT,DELETE等其他动词? 谢谢。 – recneps 2013-03-12 19:47:45

0

尝试在Web.config文件的serviceModel部分:

<system.serviceModel> 
    <services> 
     <service behaviorConfiguration="RestServiceBehavior" name="Service1"> 
     <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" 
      bindingConfiguration="webHttpBindingWithJSONP" contract="IService1" /> 
     </service> 
    </services> 

    <bindings> 
     <webHttpBinding> 
     <binding name="webHttpBindingWithJSONP"/> 
     </webHttpBinding> 
    </bindings> 

    <behaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="RestServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel>