C# 为什么我的WCF客户端引用(基于任务),生成XXXXX响应?
WCF服务合同:C# 为什么我的WCF客户端引用(基于任务),生成XXXXX响应?,c#,wcf,C#,Wcf,WCF服务合同: [ServiceContract] public interface IServerService { [OperationContract] GetServerDatetimeCallback GetServerDatetime(); } WCF服务: public class ServerService : IServerService { public GetServerDatetimeCallback GetServerDatetime()
[ServiceContract]
public interface IServerService
{
[OperationContract]
GetServerDatetimeCallback GetServerDatetime();
}
WCF服务:
public class ServerService : IServerService
{
public GetServerDatetimeCallback GetServerDatetime()
{
GetServerDatetimeCallback callback = new GetServerDatetimeCallback();
try
{
callback.ServerDatetime = DateTime.Now;
callback.Result = 1;
return callback;
}
catch (Exception ex)
{
callback.Result = 255;
return callback;
}
}
}
客户端设置:
服务地址:'net。tcp://localhost:50011/ServerService/mex'
选项:“允许生成异步操作”-选中“生成基于任务的操作”-选中
客户端调用WCF代码:
private ServerServiceClient client = new ServerServiceClient();
public async Task<GetServerDatetimeCallback> GetServerDatetimeAsync()
{
return await client.GetServerDatetimeAsync();
}
这是生成的服务引用
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName="GetServerDatetimeResponse", WrapperNamespace="http://tempuri.org/", IsWrapped=true)]
public partial class GetServerDatetimeResponse {
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", Order=0)]
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public ServerServiceObjects.GetServerDatetimeCallback GetServerDatetimeResult;
public GetServerDatetimeResponse() {
}
public GetServerDatetimeResponse(ServerServiceObjects.GetServerDatetimeCallback GetServerDatetimeResult) {
this.GetServerDatetimeResult = GetServerDatetimeResult;
}
}
我不知道为什么它会生成System.ServiceModel.MessageContractAttribute,而不是System.Runtime.Serialization.DataContractAttribute?“…在新项目中,…”很可能与“添加Servicereference”的方式有关。它有几个代码生成选项。我做了与以前项目相同的事情。1.添加服务引用;2.提供服务地址;3.提供名称空间;4.单击确定;它会自动为我生成任务库函数。但这一次,基于任务的返回值变成了“xxxResponse”……好吧,最好这样提问,但我仍然没有任何线索。尝试在一个小的一次性应用程序中(再次)复制它。请密切注意所有设置和版本。
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(WrapperName="GetServerDatetimeResponse", WrapperNamespace="http://tempuri.org/", IsWrapped=true)]
public partial class GetServerDatetimeResponse {
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", Order=0)]
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public ServerServiceObjects.GetServerDatetimeCallback GetServerDatetimeResult;
public GetServerDatetimeResponse() {
}
public GetServerDatetimeResponse(ServerServiceObjects.GetServerDatetimeCallback GetServerDatetimeResult) {
this.GetServerDatetimeResult = GetServerDatetimeResult;
}
}