wsHttpBinding无法在WCF服务中自动生成;基于SOAP的绑定确实有效

时间:2012-10-11 01:08:29

标签: wcf rest webhttpbinding

我有一个简单的WCF服务,如下所示。

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    string GetData(int value);
}

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

服务器Web.config文件是

<?xml version="1.0"?>
<configuration>
<appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="webHttpEndpointBehavior">
                <webHttp faultExceptionEnabled="true" />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="serviceBehaviourDebug">
                <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                <!-- To receive exception details in faults for debugging purposes, set the value below to true.
                     Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>

    <services>
        <service name="Diws.Service1" behaviorConfiguration="serviceBehaviourDebug">
            <endpoint
                address="/basicHttp"
                binding="basicHttpBinding"
                contract="Diws.IService1"/>
            <endpoint
                address="/webHttp"
                binding="webHttpBinding"
                behaviorConfiguration="webHttpEndpointBehavior"
                contract="Diws.IService1"/>
            <endpoint
                address="/wsHttp"
                binding="wsHttpBinding"
                contract="Diws.IService1"/>
            <endpoint
                address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />
        </service>
    </services>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

<system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
</system.web>

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
    -->
    <directoryBrowse enabled="true"/>
</system.webServer>
</configuration>

客户端是一个控制台应用程序,其App.config就是这个。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="webHttpEndpointBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    </behaviors>

    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" />
        </basicHttpBinding>
        <wsHttpBinding>
            <binding name="WsHttpBinding_IService1" />
        </wsHttpBinding>
        <webHttpBinding>
            <binding name="WebHttpBinding_IService1" />
        </webHttpBinding>
    </bindings>

    <client>
        <endpoint
            address="http://localhost:50001/Service1.svc/basicHttp"
            binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1"
            contract="ServiceReference1.IService1"
            name="BasicHttpEndpoint_IService1" />
        <endpoint
            address="http://localhost:50001/Service1.svc/webHttp"
            behaviorConfiguration="webHttpEndpointBehavior"
            binding="webHttpBinding"
            bindingConfiguration="WebHttpBinding_IService1"
            contract="ServiceReference1.IService1"
            name="WebHttpEndpoint_IService1" />
        <endpoint
            address="http://localhost:50001/Service1.svc/wsHttp"
            binding="wsHttpBinding"
            bindingConfiguration="WsHttpBinding_IService1"
            contract="ServiceReference1.IService1"
            name="WsHttpEndpoint_IService1"/>
    </client>
</system.serviceModel>

<system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
</system.web>
</configuration>

客户端程序就是这样。

class Program
{
    static void Main(String[] args)
    {
        String response = "";

        Service1Client basicHttpClient = new Service1Client("BasicHttpEndpoint_IService1");
        response = basicHttpClient.GetData(10);
        basicHttpClient.Close();
        Console.WriteLine(response);

        ///* Some communication exception
        Service1Client webHttpClient = new Service1Client("WebHttpEndpoint_IService1");
        response = webHttpClient.GetData(20);
        webHttpClient.Close();
        Console.WriteLine(response);
        //*/

        Service1Client wsHttpClient = new Service1Client("WsHttpEndpoint_IService1");
        response = wsHttpClient.GetData(30);
        wsHttpClient.Close();
        Console.WriteLine(response);

        Console.WriteLine();
        Console.WriteLine("Done");
        Console.ReadLine();
    }
}

basicHttpClient和wsHttpClient完美配合。但是,webHttpClient抛出异常“System.ServiceModel.CommunicationException未处理,HResult = -2146233087,消息=内部服务器错误”

我无法在服务器端进行调试,因为Visual Studio 2012说 “无法自动调试'MyProject'。无法调试远程过程。这通常表示尚未在服务器上启用调试。”

但是,启用了调试。在打开诊断信息时,我无法获得使用SvcTraceViewer的任何见解。

我的主要兴趣是弄清楚为什么使用WebHttpBinding的REST调用失败,但是也有助于让服务器端调试工作。我正在使用多个启动项目调试VS2012中的客户端和服务器。 Localhost是唯一涉及的服务器。

我知道REST端点不会出现在WcfTestClient中,因为它不提供元数据交换,但我希望能够通过该端点调用服务,我发现我的代码和调用RESTful WCF的示例之间没有区别服务。

1 个答案:

答案 0 :(得分:0)

要访问REST端点,请尝试使用浏览器或HttpClient向URL发出HTTP POST请求:$ http:// localhost:50001 / Service1.svc / webHttp / GetData $。当您在代码中使用webHttpClient来调用服务时,您将发送一个REST端点无法处理的SOAP请求。我相信这是你的其他两个端点工作正常但不是这个的原因。