无法从浏览器运行WCF方法

时间:2017-03-31 14:07:44

标签: c# 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/

浏览器网址行 - 测试方法:

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

感谢。

1 个答案:

答案 0 :(得分: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>

现在您可以使用EndPoint方法创建GET

[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即可。