wsHttpBinding在WCF自托管服务中不工作;基于SOAP的绑定确实有效

wsHttpBinding在WCF自托管服务中不工作;基于SOAP的绑定确实有效,wcf,rest,webhttpbinding,Wcf,Rest,Webhttpbinding,我有一个简单的WCF服务,如下所示 [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)] string GetData(int value); } public class Service1 : IService1 { public

我有一个简单的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>
basicHttpClient和wsHttpClient可以完美地工作。但是,webHttpClient引发异常“System.ServiceModel.CommunicationException未经处理,HResult=-2146233087,Message=内部服务器错误”

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

但是,已启用调试。在启用诊断功能的情况下使用SvcTraceViewer,我无法获得任何见解

我的主要兴趣是找出使用WebHttpBinding的REST调用失败的原因,但是如果能够帮助服务器端调试工作,我也会非常感激。我正在使用多个启动项目调试VS2012中的客户端和服务器。Localhost是唯一涉及的服务器


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

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

你能告诉我你的服务合同是什么样子的吗?我已经更改了地址,如下所示主机已启动终结点地址:绑定:System.ServiceModel.BasicHttpBinding约定:Diws.IService1地址:绑定:System.ServiceModel.WSHttpBinding约定:Diws.IService1地址:绑定:System.ServiceModel.WebHttpBinding约定:Diws.IService1地址:绑定:System.ServiceModel.WSHttpBinding约定:System.ServiceModel.Description.IMetadataExchange按[ENTER]终止。还将“[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]”添加到服务类中。当使用URL“”从web浏览器调用服务时,我得到了。我认为Visual Studio生成的客户端服务代理将能够处理基于SOAP和基于REST的端点,只要客户端的配置与服务器的配置匹配。这不是真的吗?客户端代理是否只处理基于SOAP的端点?我应该使用类似的方法来访问通过wsHttpBinding端点公开的基于REST的服务?我还尝试使用“[WebInvoke(Method=“GET”,BodyStyle=WebMessageBodyStyle.WrappedRequest]”或“[WebGet]”注释服务接口。两者都在浏览器戳时产生ContractFilter不匹配异常,或者在客户端代理戳时产生内部错误异常。REST端点没有wsdl。Service1Client可用于basicHttpBinding或wsHttpBinding等SOAP端点。要执行基于GET的服务操作示例,请将注释更改为WebGet。WebInvoke用于POST、PUT、Delete方法。下面是一个我可以指出的快速示例:www.c-sharpcorner.com/uploadfile/freebooksarticles/addisonwesley/2009aug25051629am/ProgrammableWeb/4.aspx。通过这样做,我能够让您的示例浏览url:localhost:8080/Service/webHttp/GetData?value=123$
<?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();
    }
}