【发布时间】:2015-06-29 05:40:42
【问题描述】:
我有一个 wcf 方法,它总是返回错误 400 - bac request。
这是界面:
[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 bad request 错误?
【问题讨论】:
-
您的端点缺少地址?
-
地址在 Global.asax 中路由如下:
private void RegisterRoutes() { RouteTable.Routes.Add(new ServiceRoute("AttachmentService", new WebServiceHostFactory(), typeof(AttachmentService))); } -
1) 尝试通过浏览器导航到它。 2) 打开服务的帮助页面,看看是否可以导航到该页面。 3)尝试使用一个或没有参数的更简单版本的服务
-
1) 当我通过浏览器导航时,页面是空白的并且断点不会触发。 2) 如何打开帮助页面? 3)我添加了一个无效的测试方法,我得到了同样的错误
-
启用帮助页面,如下所示: