WCF返回错误400错误请求

时间:2015-06-29 05:40:42

标签: c# wcf

我有一个wcf方法,总是返回错误400 - bac请求。

这是界面:

[ServiceContract(Namespace = "https://Services.xxx.com", ProtectionLevel = System.Net.Security.ProtectionLevel.None)]    
public interface IAttachmentService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "GetApplicationEntity/{appToken}/{appCode}/{entityCode}/{userName}", Method = "GET", ResponseFormat = WebMessageFormat.Xml)]
    XmlDocument GetApplicationEntity(string appToken, string appCode, string entityCode, string userName);
}

这是方法:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class AttachmentService : IAttachmentService
{
    public XmlDocument GetApplicationEntity(string appToken, string appCode, string entityCode, string userName)
    {
       //The debugger doesn't even get into this method
    }
}

我尝试过以两种不同的方式调用此方法:

public XmlDocument GetApplicationEntity(string serviceURL, string appToken, string appCode, string entityCode, string userName)
{
    WCFClientProxy<Attachment.Interfaces.IAttachmentService> proxy = new WCFClientProxy<Attachment.Interfaces.IAttachmentService>();
    return proxy.Instance.GetApplicationEntity(appToken, appCode, entityCode, userName);
}

public XmlDocument GetApplicationEntity(string serviceURL, string appToken, string appCode, string entityCode, string userName)
{
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
     request.Method = "GET";

     using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()))
     {
         XmlDocument doc = new XmlDocument();
         doc.LoadXml(reader.ReadToEnd());
         return doc;
     }
}

我正在使用basicHttpBinding:

<basicHttpBinding>
    <binding name="higherMessageSize" transferMode="Streamed" maxReceivedMessageSize="9223372036854775807"
             closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" />
</basicHttpBinding>

在服务器端使用此端点:

<endpoint binding="basicHttpBinding"
          bindingConfiguration="higherMessageSize"
          contract="Interfaces.IAttachmentService" />

和客户端的这一点:

<endpoint address="http://localhost:54893/AttachmentService.svc"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IAttachmentService"
                contract="Interfaces.IAttachmentService"
                name="BasicHttpBinding_IAttachmentService" />

我在这里做错了它总是会返回400个错误的请求错误?

1 个答案:

答案 0 :(得分:0)

我已经忘记了使用代理调用WCF服务只允许传递1个对象,为了传递几个字符串,我解决了这个问题:

public XmlDocument GetApplicationEntity(string serviceURL, string appToken, string appCode, string entityCode, string userName)
{
    try
    {
        string requestUrl = string.Format("{0}/GetApplicationEntity/{1}/{2}/{3}/{4}", serviceURL, appToken, appCode, entityCode, userName);

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
        request.Method = "GET";

        using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()))
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reader.ReadToEnd());

            return doc;
        }
    }
    catch (Exception ex)
    {
        return null;
    }
}

并从basicHttpBinding更改为webHttpBinding。这是我的配置文件:

客户端配置:

    <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="2147483647" />
  </system.web>

  <system.serviceModel>
    <bindings>
      <webHttpBinding>

        <binding name="WebHttpBinding_IAttachmentService" closeTimeout="10:00:00" openTimeout="10:00:00" receiveTimeout="10:00:00" sendTimeout="10:00:00"
                 maxBufferSize="2147483647"
                 maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647">

          <readerQuotas maxDepth="2147483647"
                        maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647"
                        maxNameTableCharCount="2147483647"
                        maxStringContentLength="2147483647"/>

          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:54893/AttachmentService.svc"
                binding="webHttpBinding"
                bindingConfiguration="WebHttpBinding_IAttachmentService"
                behaviorConfiguration="webHttpBehavior"
                contract="XXX.Interfaces.IAttachmentService" />
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
  </system.webServer>

服务配置:

<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="2147483647" executionTimeout="3600" />
  </system.web>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="XXX.Implementation.AttachmentService">
        <endpoint binding="webHttpBinding"
                  behaviorConfiguration="webHttpBehavior"
                  bindingConfiguration="higherMessageSize"
                  contract="XXX.Interfaces.IAttachmentService" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="higherMessageSize" transferMode="Streamed" closeTimeout="10:00:00" openTimeout="10:00:00" receiveTimeout="10:00:00" sendTimeout="10:00:00"
                 maxBufferSize="2147483647"
                 maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647">

          <readerQuotas maxDepth="2147483647"
                        maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647"
                        maxNameTableCharCount="2147483647"
                        maxStringContentLength="2147483647"/>

          <security mode="None"/>
        </binding>
      </webHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule"/>
    </modules>
     <handlers>
         <remove name ="WebDAV"/>
    </handlers>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483647" />
      </requestFiltering>
    </security>
  </system.webServer>