2

我们最近将一个新开发的预编译服务部署到我们的测试域并收到以下错误:

Type 'System.Threading.Tasks.Task`1[Domain.Infrastructure.Contracts.Configuration.DomainServices]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types. 

服务器是运行 .NET 4.0 的 Windows 2008R2。

对此有一些 Stack Overflow 问题,但大多数似乎是指 Async 的 CTP 版本。显然,您必须在服务器上安装 .NET 4.5 才能使用此代码。

随着 BCL.Async NuGet 包的发布,这种情况是否完全改变了?

我的印象是使用 Async 编译器编译并包含来自 NuGet 的 BCL 库的代码具有在 .NET 4 环境中运行所需的一切。

难道我们还要把服务器上的.NET运行时升级到4.5吗?

编辑:堆栈跟踪提供:

[InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[Domain.Infrastructure.Contracts.Configuration.DomainServices]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.]
System.Runtime.Serialization.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type) +1184850
System.Runtime.Serialization.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type) +787
System.Runtime.Serialization.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type) +117
System.Runtime.Serialization.XsdDataContractExporter.GetSchemaTypeName(Type type) +85
System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreatePartInfo(MessagePartDescription part, OperationFormatStyle style, DataContractSerializerOperationBehavior serializerFactory) +48
System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.CreateMessageInfo(DataContractFormatAttribute dataContractFormatAttribute, MessageDescription messageDescription, DataContractSerializerOperationBehavior serializerFactory) +708
System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter..ctor(OperationDescription description, DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory) +570
System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription operation, Boolean& formatRequest, Boolean& formatReply, Boolean isProxy) +308
System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch) +69
System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription contract, ClientRuntime proxy, DispatchRuntime dispatch) +120
System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +4250
System.ServiceModel.ServiceHostBase.InitializeRuntime() +82
System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +64
System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +789
System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +255
System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +1172

[ServiceActivationException: The service '/Services/Binary/Endpoint.svc' cannot be activated due to an exception during compilation.  The exception message is: Type 'System.Threading.Tasks.Task`1[Domain.Infrastructure.Contracts.Configuration.DomainServices]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types..]
System.Runtime.AsyncResult.End(IAsyncResult result) +901504
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +178638
System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +107
4

1 回答 1

8

好的,我将在这里很好地猜测您正在尝试公开一个异步 WCF 操作,该操作返回一个Task<Domain.Infrastructure.Contracts.Configuration.DomainServices>. 虽然 Microsoft.Bcl.Async 将允许您编译使用任务的代码,但它不会为 WCF 提供允许您在服务中使用任务的 .NET Framework 4.5 更改。

话虽如此,您仍然可以使用异步编程模型向 WCF 公开异步方法,同时仍然使用 TPL 编写代码。为此,您必须使用 APM 开始/结束方法包装该方法。像这样的东西:

[ServiceContractAttribute]
public interface ISampleService
{
    [OperationContractAttribute]
    string SampleMethod();

    [OperationContractAttribute(AsyncPattern = true)]
    IAsyncResult BeginSampleMethod(AsyncCallback callback, object asyncState);

    string EndSampleMethod(IAsyncResult result);
}

public class SampleService : ISampleService
{
    // the async method needs to be private so that WCF doesn't try to
    // understand its return type of Task<string>
    private async Task<string> SampleMethodAsync()
    {
        // perform your async operation here
    }

    public string SampleMethod()
    {
        return this.SampleMethodAsync().Result;
    }

    public IAsyncResult BeginSampleMethod(AsyncCallback callback, object asyncState)
    {
        var task = this.SampleMethodAsync();
        if (callback != null)
        {
            task.ContinueWith(_ => callback(task));
        }

        return task;
    }

    public string EndSampleMethod(IAsyncResult result)
    {
        return ((Task<string>)result).Result;
    }
}
于 2013-08-08T05:44:05.143 回答