简体   繁体   中英

WCF returns error 400 bad request

I have a wcf method that always returns the error 400 - bac request.

This is the interface:

[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);
}

This is the method:

[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
    }
}

I've tried calling this method in 2 different ways:

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);
}

And

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;
     }
}

I'm using basicHttpBinding like this:

<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>

with this endpoint on the server side:

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

and this rnd point on the client side:

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

What am I doing wrong here that it always returns this 400 bad request error ?

Well I've forgot that call the WCF service using the proxy allows to pass only 1 object and in order to pass several string I solved it like this:

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;
    }
}

and changed from basicHttpBinding to webHttpBinding . Here are my config files:

Client config:

    <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>

Service config:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM