2017-03-31 61 views
1

我创建了一个简单的WCF服务应用程序,并尝试更改WCF代码,它也将能够从客户端浏览器中调用,但它只能成功从WCF运行客户。无法从浏览器运行WCF方法

从浏览器运行,不调用WCF服务(但在浏览器中没有错误消息)。

代码:

合同:

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    [WebGet] 
    string Test(); 

    [OperationContract] 
    [WebGet] 
    string GetData(int value); 
} 

实现:

public class Service1 : IService1 
{ 
    public string GetData(int value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 

    public string Test() 
    { 
     return "test success"; 
    } 
} 

Web.config文件:

<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <protocolMapping> 
     <add binding="webHttpBinding" scheme="https" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 
</configuration> 

项目网址:

http://localhost:1507/ 

浏览器URL行 - 测试方法:

http://localhost:1507/Service1.svc/Test 

感谢。

+0

你是什么意思“从浏览器运行”?运行什么? 'Test'是一个操作,而不是一个URL。你不能用GET调用它。这不是WCF的限制,这正是(SOAP)Web服务的工作原理。也许您是否将Web服务与REST API混淆? –

+0

因为WCF服务从本地主机运行,我从本地浏览器运行“http:// localhost:1507/Service1.svc/Test”行。 –

+0

我将绑定更改为binding =“webHttpBinding”,它将使用REST API运行。 –

回答

1

起初您需要开发一项宁静的服务。请按照此配置:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 

    <appSettings> 
<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="SoapBinding" /> 
     </basicHttpBinding> 
     <mexHttpBinding> 
     <binding name="MexBinding" /> 
     </mexHttpBinding> 
     <webHttpBinding> 
     <binding name="RestBinding" /> 
     </webHttpBinding> 
    </bindings> 

    <client /> 

    <services> 
     <service behaviorConfiguration="ServiceBehavior" name="YourServiceName.Service1 "> 
     <endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="SoapBinding" name="Soap" contract="EyeMan.IService1 " /> 
     <endpoint address="" behaviorConfiguration="Web" binding="webHttpBinding" bindingConfiguration="RestBinding" name="Rest" contract="EyeMan.IService1 " /> 
     <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="MexBinding" name="Mex" contract="IMetadataExchange" /> 
     </service> 
    </services> 

    <behaviors> 
     <endpointBehaviors> 
     <behavior name="Web"> 
      <webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" /> 
     </behavior> 
     </endpointBehaviors> 

     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 
</configuration> 

现在你可以创建一个EndPointGET方法:

[OperationContract] 
[WebInvoke(Method = "GET", UriTemplate = @"/GetData?value={value}")] 
public string GetData(int value) 
{ 
    return string.Format("You entered: {0}", value); 
} 
[OperationContract] 
[WebInvoke(Method = "GET", UriTemplate = @"/Test")] 
public string Test() 
{ 
    return "test success"; 
} 

现在你是好去。请在您的浏览器上拨打http://localhost:1507/Service1.svc/Test