从Web浏览器调用WCF服务方法

时间:2014-10-28 14:04:37

标签: c# wcf rest azure web

我想从网络浏览器运行我的服务:http://localhost:443/TestService//RunTest/data/test 它对我不起作用

This page can’t be displayed

•Make sure the web address http://localhost:443 is correct.
•Look for the page with your search engine.
•Refresh the page in a few minutes.

如何解决 - 重新定义端点 - 如何? WCF服务:

//TestService.svc.cs
public class TestService : ITestService
    {
        public string RunTest(string data)
        {
            return string.Format("You entered: {0}", data);
        }
}
//ITestService.cs
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/RunTest/data/{data}")]
string RunTest(string data)
{
     return string.Format("You entered: {0}", proxyDomain);
}
//Web.config
<system.serviceModel>
    <services>
      <!-- This section is optional with the default configuration introduced
         in .NET Framework 4.5.1 -->
      <service
          name="TestService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:443/TestService/"/>
          </baseAddresses>
        </host>
        <endpoint address="http://localhost:443/TestService"
                  binding="wsHttpBinding"
                  contract="ITestService" />
      </service>
    </services>
</system.serviceModel>

当我运行它时,使用端口54388打开WCF客户端

2 个答案:

答案 0 :(得分:0)

根据我的经验,您无法通过浏览器直接测试。 相反,您应该使用WCF测试客户端:

http://msdn.microsoft.com/en-us/library/bb552364(v=vs.110).aspx

使用浏览器可以做的是查看是否可以访问WSDL。 在浏览器中调用WSDL的示例:

http://localhost:8080/DecisionService/ws/PreTradeChecksRuleApp/1.0/PreTradeChecks/1.0?WSDL 

答案 1 :(得分:0)

您应该添加/更改端点&#34;绑定&#34;属性为&#34; webHttpBinding&#34;像这样:

 <endpoint address="http://localhost:443/TestService"
              binding="webHttpBinding"
              contract="ITestService" behaviorConfiguration="web" />

或者

 <endpoint address="http://localhost:443/TestService"
              binding="wsHttpBinding"
              contract="ITestService" />
 <endpoint address="http://localhost:443/TestService/web"
              binding="webHttpBinding"
              contract="ITestService" behaviorConfiguration="web"/>

您必须添加两个案例:

<behaviors>
...
<endpointBehaviors>
            <behavior name="web">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
...
</behaviors>

备注:

1)您必须将属性behaviorConfiguration添加到webBinding并添加行为配置

2)在第二个解决方案中,您必须更改baseAddress的名称,因为您无法将两个端点配置到同一个baseAddress(如果有人知道如何操作它,请它会帮助我也是

最好的问候