WCF服务合同:
[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;
}
}
}
客户端设置:
ServiceAddress:' net.tcp:// localhost:50011 / ServerService / mex' 选项:'允许生成异步操作' - 检查,'生成基于任务的操作' - 选择
客户端调用WCF代码:
private ServerServiceClient client = new ServerServiceClient();
public async Task<GetServerDatetimeCallback> GetServerDatetimeAsync()
{
return await client.GetServerDatetimeAsync();
}
通常我会像这样调用wcf函数async,并且总是正常工作。 但今天我又在一个新项目中再做一次,我找到了等待client.GetServerDatetimeAsync();&#39;不再返回&#39; GetServerDatetimeCallback&#39;对象,但是&#39; GetServerDatetimeResponse&#39;。
这是怎么发生的?
UPDATED GetServerDatetimeCallback对象
[DataContract]
public class GetServerDatetimeCallback
{
[DataMember]
public byte Result { get; set; }
[DataMember]
public DateTime ServerDatetime { get; set; }
}
这是服务参考生成
[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?