Seb*_*ski 5 c# wcf serialization datacontractserializer
使用WCF,假设以下服务接口:
[ServiceContract]
public interface IForDataTypeA
{
[OperationContract]
List<DataTypeA> GetValues();
}
[DataContract]
public class DataTypeA { }
Run Code Online (Sandbox Code Playgroud)
这是它在服务器端(服务主机)定义的方式.
现在,某人 - 无法访问服务器的源代码 - 尝试使用此WCF服务.他自己定义了服务接口,但不小心将数据类型的名称更改为DataTypeB:
[ServiceContract]
public interface IForDataTypeA
{
[OperationContract]
List<DataTypeB> GetValues();
}
[DataContract]
public class DataTypeB { }
Run Code Online (Sandbox Code Playgroud)
当他现在调用GetValues()(via ChannelFactory<IForDataTypeA>)时,返回的列表将始终为空,但不会抛出任何异常.
当列表中的元素无法反序列化(而不是返回空列表)时,有没有办法让WCF抛出异常?
这是重现问题的完整代码:
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
namespace TestProj
{
[DataContract]
public class DataTypeA { }
[DataContract]
public class DataTypeB { }
[ServiceContract]
public interface IForDataTypeA
{
[OperationContract]
List<DataTypeA> GetValues();
}
[ServiceContract(Name = "IForDataTypeA")]
public interface IForDataTypeB
{
[OperationContract]
List<DataTypeB> GetValues();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
internal class ServiceImpl : IForDataTypeA
{
public List<DataTypeA> GetValues()
{
return new List<DataTypeA>
{
new DataTypeA(),
new DataTypeA(),
};
}
}
class Program
{
static void Main()
{
var serviceHost = new ServiceHost(new ServiceImpl());
var binding = new NetTcpBinding();
var endpointUrl = "net.tcp://127.0.0.1:5555/MyService";
serviceHost.AddServiceEndpoint(typeof(IForDataTypeA), binding, endpointUrl);
serviceHost.Open();
var channelFactory = new ChannelFactory<IForDataTypeB>(binding, endpointUrl);
var channel = channelFactory.CreateChannel();
List<DataTypeB> values = channel.GetValues();
// values will always be empty
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
591 次 |
| 最近记录: |