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 クライアントでは問題なく機能するようです。
ただし、たとえば WebClient UploadString を使用して、非 Wcf クライアントから呼び出しを行うと (サービス参照を使用できることはわかっています。これはテスト目的です)、返されます。
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ファイルでは、この例から取られたように、非wcfクライアントに対してsoap障害をスローする以下を追加しました
<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>
どこが間違っていますか?