他们可能知道也可能不知道。当DateTime 对象在客户端和服务器之间传递时,它们被序列化为双方都能理解的某种通用格式。在其中一些格式中,例如 XML,时区信息是通过网络发送的:如果您有一个 DateTime 和 DateTimeKind.Utc,则将在序列化日期后附加一个“Z”;对于Local,将添加时区,对于Unspecified,将不添加任何内容,因此对方知道使用哪种格式。对于其他格式,例如JSON,如果类型是Utc,服务器将不会在日期时间发送任何内容,但会添加其他类型的本地时区信息(Local的JSON格式没有区别和Unspecified;IIRC 接收方会将此类信息视为Local)。
如果您想查看网络上发生了什么,您可以运行下面的程序,同时拥有 Fiddler 等网络捕获工具,以查看客户端向服务器发送的内容。
public class StackOverflow_14132566
{
[ServiceContract]
public interface ITest
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
DateTime Add10Hours(DateTime input, string description);
}
public class Service : ITest
{
public DateTime Add10Hours(DateTime input, string description)
{
return input.AddHours(10);
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "basic");
host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "web").Behaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress + "/basic"));
ITest proxy = factory.CreateChannel();
Console.WriteLine(proxy.Add10Hours(new DateTime(2013, 1, 2, 19, 29, 0, DateTimeKind.Utc), "XML, UTC"));
Console.WriteLine(proxy.Add10Hours(new DateTime(2013, 1, 2, 19, 29, 0, DateTimeKind.Local), "XML, Local"));
Console.WriteLine(proxy.Add10Hours(new DateTime(2013, 1, 2, 19, 29, 0, DateTimeKind.Unspecified), "XML, Unspecified"));
((IClientChannel)proxy).Close();
factory.Close();
factory = new ChannelFactory<ITest>(new WebHttpBinding(), new EndpointAddress(baseAddress + "/web"));
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
proxy = factory.CreateChannel();
Console.WriteLine(proxy.Add10Hours(new DateTime(2013, 1, 2, 19, 29, 0, DateTimeKind.Utc), "JSON, UTC"));
Console.WriteLine(proxy.Add10Hours(new DateTime(2013, 1, 2, 19, 29, 0, DateTimeKind.Local), "JSON, Local"));
Console.WriteLine(proxy.Add10Hours(new DateTime(2013, 1, 2, 19, 29, 0, DateTimeKind.Unspecified), "JSON, Unspecified"));
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}