简体   繁体   English

在WCF REST中使用DataContractSurrogate

[英]Using a DataContractSurrogate with WCF REST

How can I use a DataContractSurrogate for my WCF REST service (hosted using a WebServiceHostFactory)? 如何为WCF REST服务(使用WebServiceHostFactory托管)使用DataContractSurrogate?

I don't see a way of adding one and even if I add a custom IOperationBehavior, the WebServiceHost automatically overwrites and ignores it. 我看不到添加一个的方法,即使我添加了自定义IOperationBehavior,WebServiceHost也会自动覆盖并忽略它。

You can achieve this with the following two steps: 您可以通过以下两个步骤来实现:

First, implement IDatacontractSurrogate interface: 首先,实现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...
}

Second, set your surrogate on the ServiceHost like this: 其次,像这样在ServiceHost上设置代理:

foreach (var endpoint in serviceHost.Description.Endpoints)
{
    foreach (var operation in endpoint.Contract.Operations)
    {
         operation.Behaviors.Find<DataContractSerializerOperationBehavior>().DataContractSurrogate = new MySurrogate();
    }
}

Remember to do this before you open your service host. 在打开服务主机之前,请记住执行此操作。 Otherwise it may not work. 否则可能无法正常工作。

In case you're using IIS hosting and specifying the WebServiceHostFactory in an .svc file, then understandably, you don't have the opportunity set the surrogate. 如果您使用IIS托管并在.svc文件中指定WebServiceHostFactory ,那么可以理解,您没有机会设置代理。 To overcome that, you have two options: 为了克服这个问题,您有两种选择:

  1. Create a custom service behavior attribute and set the surrogate in its ApplyDispatchBehavior() method. 创建一个自定义服务行为属性,并在其ApplyDispatchBehavior()方法中设置代理。 Once you place this attribute on your sevice, WCF will automatically execute this method and the surrogate will be set. 一旦将此属性放在您的服务上,WCF将自动执行此方法并设置代理。

  2. Create your own custom service host by subclassing WebServiceHost . 通过子类化WebServiceHost 创建自己的定制服务主机。 Then set the surrogate in its ApplyConfiguration() method. 然后在其ApplyConfiguration()方法中设置代理。 This too will have the same effect. 这也将具有相同的效果。

I managed to get it working: WCF 4.0 REST service hosted using a WebServiceHostFactory in IIS. 我设法使其工作:使用IIS中的WebServiceHostFactory托管的WCF 4.0 REST服务。

I used a custom attribute to inject my NHProxyDataContractSurrogate: 我使用了一个自定义属性来注入我的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) { }
}

And applied the custom attribute to my ServiceContract: 并将自定义属性应用于我的ServiceContract:

[ServiceContract]
[CanSerializeNHProxy]
public interface IElementManager
{ ... }

I got a lot of useful info from these links: 这些链接提供了很多有用的信息:

DataContractSurrogate MSDN page, pointing to custom attribute DataContractSurrogate MSDN页面,指向自定义属性

DataContractSurrogate implementation for serializing NHibernate proxy objects 用于序列化NHibernate代理对象的DataContractSurrogate实现

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM