2017-02-07 9 views
1

私はWCFバックエンドとUWPフロントエンドを持つC#アプリケーションを持っています。私は私のサービス契約を実装するサービスを持っています。サービス上で例外を捕捉し、フォルト例外としてそれらを再スローすると、すべてが期待通りに機能します。RealProxyのサービス契約からWCFフォールト例外をスローする

これで、RealProxyのインスタンスを使用してサービスコールをラップし、エラー例外をプロキシからスローすることで、サービスコードをクリーンアップしようとしています。残念ながら、サービスに例外がスローされたとき、クライアントは次のメッセージで例外を受け取ります。

"サーバーは意味のない返答をしませんでした。これは、契約の不一致、セッションの早期終了または内部サーバーエラーです。次の実装では

[ServiceContract(Namespace = @"blah/blah/blah", SessionModel = SessionMode.Required) 
public interface IService1 
{ 
    [OperationContract] 
    [FaultContract(typeof(Exception))] 
    bool DoSomething(Setting setting); 
} 

は、私は、次のサービス契約を持っている

[Export(typeof(IService1)] 
[ServiceBehavior(InstanceContextMode = IntanceContextMode.Single, 
    ConcurrencyMode = ConcurrencyModel.Single] 
public class Service1 : IService1 
{ 
    bool DoSomthing(Setting setting) 
    { 
     try 
     { 
      throw new ArgumentException("setting"); 
     } 
     catch (Exception ex) 
     { 
      throw WrapException(ex); 
     } 
    } 

    private FaultException<Exception> WrapException(Exception ex) 
    { 
     string message = exception.GetBaseException().Message; 
     Console.WriteLine(string.Format("Exception: {0}", message)); 
     return new FaultException<Exception>(exception, new FaultReason(message)); 
    } 
} 

私のWCFサービスホストは、クライアント側の設定を対応して、以下の構成を有しています。

public class ServiceProxy<T> : RealProxy 
    { 
     private readonly T _instance; 

     private ServiceProxy(T instance) 
     : base(typeof(T)) 
     { 
      _instance = instance; 
     } 

     public static T Create(T instance) 
     { 
      return (T) new ServiceProxy<T>(instance).GetTransparentProxy(); 
     } 

     public override IMessage Invoke(IMessage msg) 
     { 
      var methodCall = (IMethodCallMessage) msg; 
      var method = (MethodInfo) methodCall.MethodBase; 

      try 
      { 
       var result = method.Invoke(_instance, methodCall.InArgs); 
       return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall); 
      } 
      catch (Exception ex) 
      { 
       string message = ex.GetBaseException().Message; 
       Console.WriteLine(string.Format("Exception: {0}", message)); 
       var wrapped = new FaultException<Exception>(ex, new FaultReason(message)); 

       return new ReturnMessage(wrapped, msg as IMethodCallMessage); 
      } 
     } 
    } 

(おかげでアイデアのために@bradmoする。)

Dynamically creating a proxy class

私は私のようプロキシを使用しています:今、私はプロキシを作成しました

<system.serviceModel> 
    <services> 
     <service name="Host.Services.Service1"> 
     <endpoint address="net.tcp://localhost:8082/Service1" 
      binding="netTcpBinding" 
      contract="Host.Domain.IService1"/> 
     </service> 
    </services> 
    <bindings> 
     <netTcpBinding> 
     <binding maxReceivedMessageSize="1000000"> 
      <security mode="None"></security> 
     </binding> 
     </netTcpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

サービス:

static void Main(string[] args) 
{ 
    try 
    { 
     var service = new Bootstrapper().Run().GetExport<IService1>().Value; 
     IService1 proxy = ServiceProxy<IService1>.Create(service); 

      using (var host = new ServiceHost(proxy)) 
      { 
       host.Open(); 
       Console.WriteLine("Running..."); 
       Console.ReadLine(); 
      } 
    } 
    catch (Exception ex) 
    { 
      Console.WriteLine(string.Format("Exception: {0}", ex.GetBaseException().Message)); 
      Console.ReadLine(); 
    } 
    } 
} 

例外を投げるためにプロキシを使用せずに例外がスローされ、プロキシが正常に動作しますが、プロキシを使用して例外をラップすると、クライアント側で次のエラーが発生します。

"意味のある返答。これは、契約の不一致、早期セッションのシャットダウンまたは内部サーバーエラーによって引き起こされている可能性があります。」

答えて

0

問題はあなたのFaultContractが定義されている方法であるように思われる。

これらの線に沿って何かをやってみてどうかを確認それが役立ちます:

public interface IService 
{ 
    [OperationContract] 
    [FaultContract(typeof(WrappedExceptionFault))] 
    bool DoSomething(string setting); 
} 

[DataContract] 
public class WrappedExceptionFault 
{ 
    [DataMember] 
    public string Message { get; set; } 
} 

public class Service : IService 
{ 
    public bool DoSomething(string setting) 
    { 
     try 
     { 
      throw new ArgumentException(setting); 
     } 
     catch (Exception ex) 
     { 
      throw WrapException(ex); 
     } 
    } 

    private FaultException<WrappedExceptionFault> WrapException(Exception ex) 
    { 
     string message = ex.GetBaseException().Message; 
     Console.WriteLine(string.Format("Exception: {0}", message)); 
     WrappedExceptionFault fault = new WrappedExceptionFault() 
     { 
      Message = message, 
     }; 

     return new FaultException<WrappedExceptionFault>(fault, new FaultReason(message)); 
    } 
} 

をも:あなたは「を使用し」節の周りにあなたのServiceHostをラップするべきではありません..あなただけの大きなトラブルを求めている。このような何かをやって考えてみましょう:。

 ServiceHost host = new ServiceHost(proxy); 
     host.Open(); 
     Console.WriteLine("Running..."); 
     Console.ReadLine(); 

     try 
     { 
      host.Close(); 
     } 
     catch 
     { 
      host.Abort(); 
     } 
+0

あなたの答えをありがとう。サービスホストを '使用する 'ので囲むのはなぜ悪いですか? –

+0

ウェブ上には、ウェブ上に多数のドキュメントがあります。理由を説明する記事がいくつかあります:https://philmunro.wordpress.com/2011/03/07/wcf-using-gotcha/ && https://msdn.microsoft.com/en-us/library/aa355056( v = vs.110).aspx –

+0

それはトリックを行った。ありがとう。 –

関連する問題