WCF RESTサービス(WebServiceHostFactoryを使用してホスト)にDataContractSurrogateを使用するにはどうすればよいですか?WCF RESTでのDataContractSurrogateの使用
カスタムIOperationBehaviorを追加しても、WebServiceHostは自動的に上書きして無視します。
WCF RESTサービス(WebServiceHostFactoryを使用してホスト)にDataContractSurrogateを使用するにはどうすればよいですか?WCF RESTでのDataContractSurrogateの使用
カスタムIOperationBehaviorを追加しても、WebServiceHostは自動的に上書きして無視します。
あなたは次の2つの手順でこれを達成することができます:IDatacontractSurrogateインタフェースを実装し、
まず:
class MySurrogate : IDataContractSurrogate
{
public Type GetDataContractType(Type type)
{
//Implementation here
}
public object GetObjectToSerialize(object obj, Type targetType)
{
//Implementation here
}
//Implemenentation of the remaining methods...
}
第二には、このようのServiceHostにあなたの代理を設定します。
foreach (var endpoint in serviceHost.Description.Endpoints)
{
foreach (var operation in endpoint.Contract.Operations)
{
operation.Behaviors.Find<DataContractSerializerOperationBehavior>().DataContractSurrogate = new MySurrogate();
}
}
覚えておいてくださいサービスホストを開く前にこれを行う必要があります。さもなければそれは働かないかもしれない。
IISホスティングを使用して.svc
ファイルにWebServiceHostFactory
を指定している場合は、サロゲートを設定する機会はありません。それを克服するには、次の2つのオプションがあります。
をカスタムサービスの振る舞い属性を作成し、そのApplyDispatchBehavior()
方法でサロゲートを設定します。この属性をサービスに配置すると、WCFは自動的にこのメソッドを実行し、サロゲートが設定されます。
CreateサブサービスWebServiceHost
によって独自のカスタムサービスホスト。次に、ApplyConfiguration()
メソッドで代理を設定します。これも同じ効果があります。
IISでWebServiceHostFactoryを使用してホストされているWCF 4.0 RESTサービス。
私は私のNHProxyDataContractSurrogateを注入するカスタム属性を使用:
public class CanSerializeNHProxyAttribute : Attribute, IContractBehavior
{
public void ApplyClientBehavior(ContractDescription description, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime proxy)
{
foreach (OperationDescription opDesc in description.Operations)
{
ApplyDataContractSurrogate(opDesc);
}
}
public void ApplyDispatchBehavior(ContractDescription description, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.DispatchRuntime dispatch)
{
foreach (OperationDescription opDesc in description.Operations)
{
ApplyDataContractSurrogate(opDesc);
}
}
private static void ApplyDataContractSurrogate(OperationDescription description)
{
DataContractSerializerOperationBehavior dcsOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (dcsOperationBehavior != null)
{
if (dcsOperationBehavior.DataContractSurrogate == null)
dcsOperationBehavior.DataContractSurrogate = new NHProxyDataContractSurrogate();
}
}
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint serviceEndPoint, BindingParameterCollection parameters) { }
public void Validate(ContractDescription contractDescription, ServiceEndpoint serviceEndPoint) { }
}
をそして、私のServiceContractにカスタム属性を適用:
[ServiceContract]
[CanSerializeNHProxy]
public interface IElementManager
{ ... }
私はこれらのリンクから有益な情報の多くを得た:
DataContractSurrogate MSDN page, pointing to custom attribute
DataContractSurrogate implementation for serializing NHibernate proxy objects
希望します。
WebServiceHostは、カスタムDataContractSerializerOperationBehaviorを適用してもそれを聞きません。それを無視して上書きします。 – Jeff
それは奇妙です。上記の方法で私の場合は完全に動作します。ホストが開かれる前に代理人を設定しているとJeffは確信していますか? –
うーん、確かにもう一度やってみよう...しかし、これは既知の問題だと思う...ありがとう。 – Jeff