2017-03-31 59 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