【问题标题】:invoke WCF service method from web browser从 Web 浏览器调用 WCF 服务方法
【发布时间】:2014-10-28 14:04:37
【问题描述】:

我想从网络浏览器运行我的服务: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 客户端

【问题讨论】:

    标签: c# wcf rest azure web


    【解决方案1】:

    根据我的经验,您无法直接通过浏览器进行测试。 相反,您应该使用 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 
    

    【讨论】:

      【解决方案2】:

      您应该像这样将端点“绑定”属性添加/更改为“webHttpBinding”:

       <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(如果有人知道该怎么做,请它也会帮助我)

      最好的问候

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-21
        • 1970-01-01
        • 1970-01-01
        • 2015-10-24
        • 1970-01-01
        • 1970-01-01
        • 2013-01-14
        • 1970-01-01
        相关资源
        最近更新 更多