我创建了一个Wcf服务。它将由Wcf客户端和非Wcf客户端访问。我为FaultException处理创建了自己的类,如下所示;
[DataContract]
public class ErrorResponse
{
[DataMember]
public string ErrMsg {get;set;}
}
对于我的服务界面,我有
[ServiceContract]
public interface IService
{
[OperationContract]
[FaultContract (typeof(ErrorResponse))]
[WebInvoke(Method = "POST", UriTemplate = "/XML/GetTypes", BodyStyle = WebMessageBodyStyle.Bare)]
TypeResponse XMLTypes(TypeRequest TypeRequest);
}
在我的方法XmlTypes中,我有以下内容;
public static TypeResponse XmlTypes(TypeRequest TypeRequest)
{
//do something
//raise a error
ErrorResponse oErrorResponse = new ErrorResponse();
oErrorResponse.ErrMsg = "Some Error happened";
FaultCode oFaultCode = new FaultCode("12345");
throw new FaultException<ErrorResponse>(oErrorResponse , new FaultReason ("Reason for the fault"),
new FaultCode("TypeRequestFailed", new FaultCode("TypeNotFound")));
这似乎适用于Wcf客户端。
然而,当从非Wcf客户端进行调用时,例如使用WebClient UploadString(我知道我可以使用服务引用,这是出于测试目的),我回来了
System.Net.WebException:远程服务器返回错误:(400) 不良请求。
这是我在另一个测试应用中的webclient代码;
WebClient oClient = new WebClient();
oClient.Encoding = UTF8Encoding.UTF8;
oClient.Headers.Add("Content-Type", "application/xml");
try
{
txtResponse.Clear();
sRequest = "<TypeRequest><UserId>1</UserId><Password>asdax12</Password></TypeRequest>";
txtResponse.Text = oClient.UploadString("http://localhost:49562/Service.svc/XML/XmlTypes", "POST", sRequest).ToString();
}
catch (Exception ex)
{
txtResponse.Text = ex.ToString();
}
在我的webconfig文件中,我添加了以下示例throwing soap faults for non wcf clients
<system.serviceModel>
<bindings>
<customBinding>
<binding name="basicHttpSoap12Binding">
<textMessageEncoding messageVersion="Soap12"/>
<httpTransport/>
</binding>
</customBinding>
</bindings>
<services>
<service name="MySoap12Service">
<endpoint address="" binding="customBinding" bindingConfiguration="basicHttpSoap12Binding"
bindingNamespace="MySoap12ServiceNamespace"
contract="MySoap12Service">
</endpoint>
</service>
</services>
</system.serviceModel>
我哪里错了?
答案 0 :(得分:1)
您是否有其他可以称为成功的网络方法? (来自同一个非WCF客户端)
由于你有一个400 http的错误,在我看来,呼叫没有以正确的方式构建或完成,所以如果你能成功调用同一服务中的任何其他方法,这将有助于我们确保呼叫是正确的。