gpt4 book ai didi

c# - 无法为引用 BCL.Async 的 Web 应用程序序列化任务 1

转载 作者:行者123 更新时间:2023-11-30 19:42:06 26 4
gpt4 key购买 nike

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

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 包的发布是否改变了这种情况?

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

服务器上的.NET runtime还是要升级到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

最佳答案

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

也就是说,您仍然可以使用 asynchronous programming model向 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;
}
}

关于c# - 无法为引用 BCL.Async 的 Web 应用程序序列化任务 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18118345/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com