[英]WCF returns error 400 bad request
我有一個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" />
這是客戶端的rnd點:
<endpoint address="http://localhost:54893/AttachmentService.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IAttachmentService"
contract="Interfaces.IAttachmentService"
name="BasicHttpBinding_IAttachmentService" />
我在這里做錯了什么,它總是返回這個400錯誤的請求錯誤?
好吧,我忘記了使用代理調用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.