这是WCF配置导致我的400错误请求吗

本文关键字:错误 请求 我的 WCF 配置 这是 | 更新日期: 2023-09-27 18:20:29

我有一个WCF应用程序,作为Azure中的网络角色托管,具有以下配置。当我试图访问浏览器中的三个服务wsdl中的任何一个或试图设置代理时,我会收到一个400坏请求。

<?xml version="1.0"?>
    <configuration>
        <appSettings>
        </appSettings>
        <system.web>
            <customErrors mode="Off"></customErrors>
            <compilation debug="true" targetFramework="4.0" />
        </system.web>
        <connectionStrings></connectionStrings>
        <system.diagnostics>      
        <sharedListeners> 
          <add name="AzureLocalStorage" type="Example.AzureLocalStorageTraceListener, Example"/> 
        </sharedListeners> 
        <sources> 
          <source name="System.ServiceModel" switchValue="Verbose, ActivityTracing"> 
            <listeners> 
              <add name="AzureLocalStorage" /> 
            </listeners> 
          </source> 
          <source name="System.ServiceModel.MessageLogging" switchValue="Verbose"> 
            <listeners> 
              <add name="AzureLocalStorage" /> 
            </listeners> 
          </source> 
        </sources>  
       </system.diagnostics>
        <system.serviceModel>
            <services>
                <service name="Service1" behaviorConfiguration="MetaBehavior">
                    <endpoint address="http://example.com/service1.svc" binding="basicHttpBinding" name="basicEndpoint1" contract="IService1" />
                </service>
                <service name="Service2" behaviorConfiguration="MetaBehavior">
                    <endpoint address="http://example.com/service2.svc" binding="basicHttpBinding" name="basicEndpoint2" contract="IService2" />
                </service>
                <service name="Service3" behaviorConfiguration="MetaBehavior">
                    <endpoint address="http://pexample.com/service3.svc" binding="basicHttpBinding" name="basicEndpoint3" contract="IService3" />
                </service>
            </services>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="MetaBehavior">
                        <serviceDebug includeExceptionDetailInFaults="true" />
                        <serviceMetadata httpGetEnabled="true"/>
                        <serviceThrottling maxConcurrentSessions="90" />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
            <serviceHostingEnvironment multipleSiteBindingsEnabled="false" aspNetCompatibilityEnabled="true" />
        </system.serviceModel>
        <system.webServer>
            <modules runAllManagedModulesForAllRequests="true"/>
        </system.webServer>
    </configuration>

我很确定我的配置不正确,但我需要一点关于什么是不正确的指导。

接口定义为:

[ServiceContract(Name = "Service1", Namespace = "http://example.com")]
public interface IService1
{
    [WebGet]
    [OperationContract]
    Result Create();
} 

这是WCF配置导致我的400错误请求吗

您使用了错误的绑定,请尝试webHttpBinding而不是basicHttpBinding。您的合约设置为WebGet,这是WCF对准REST服务的接受。BasicHttpBinding仅适用于基于soap的绑定(因此出现"Bad request"异常)。

编辑:由于存在WebGet,我认为您不需要soap端点。下面是一个同时支持soap和WebGet的配置。我不知道Azure与标准IIS有多大区别,但您可能应该为您的服务使用相对地址。IIS将只支持服务配置中的相对地址。

<system.serviceModel>
    <services>
        <service name="Service1" behaviorConfiguration="Service.Behavior">
            <endpoint address="Service1"
                      binding="basicHttpBinding"
                      contract="IService1"
                      bindingNamespace = "http://example.com"
                      bindingConfiguration="HttpBasic" />
            <endpoint address="mexService1"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange"
                      bindingNamespace = "http://example.com"/>
            <endpoint address="webService1"
                      binding="webHttpBinding"
                      behaviorConfiguration="webBehavior"
                      contract="IService1"
                      bindingNamespace = "http://example.com"
                      name="webHttp"
                      listenUriMode="Explicit" />
        </service>
        <service name="Service2" behaviorConfiguration="Service.Behavior">
            <endpoint address="Service2"
                      binding="wsHttpBinding"
                      contract="IService2"
                      bindingNamespace = "http://example.com"
                      bindingConfiguration="HttpStandard" />
            <endpoint address="mexService2"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange"
                      bindingNamespace = "http://example.com"/>
            <endpoint address="webService2"
                      binding="webHttpBinding"
                      behaviorConfiguration="webBehavior"
                      contract="IService2"
                      bindingNamespace = "http://example.com"
                      name="webHttp"
                      listenUriMode="Explicit" />
    </services>     
    <behaviors>
        <endpointBehaviors>
            <behavior name="webBehavior" >
                <webHttp />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="Service.Behavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <bindings>
        <basicHttpBinding>
            <binding name="HttpBasic" receiveTimeout="00:10:00" maxReceivedMessageSize="2048000">
                <security mode="None"/>
            </binding>
        </basicHttpBinding>
        <wsHttpBinding>
            <binding name="HttpStandard" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2048000">
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
                    <message clientCredentialType="None" negotiateServiceCredential="false" algorithmSuite="Default" establishSecurityContext="false" />
                </security>
            </binding>
            <binding name="Https" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2048000">
                <security mode="Transport">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
                    <message clientCredentialType="None" negotiateServiceCredential="false" algorithmSuite="Default" establishSecurityContext="false" />
                </security>
            </binding>
            <binding name="HttpsAuthenticated" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2048000">
                <security mode="Transport">
                    <transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
</system.serviceModel>