C# 在WCF中创建自定义可空类实现
编程很有趣 我创建了自己的可空类实现,如下所示:C# 在WCF中创建自定义可空类实现,c#,.net,wcf,xml-serialization,iis-express,C#,.net,Wcf,Xml Serialization,Iis Express,编程很有趣 我创建了自己的可空类实现,如下所示: [DataContract] public class Nullable<T> where T : struct { public Nullable() { } internal T value; [DataMember] public bool HasValue { get; set; } [DataMember] public T Value {
[DataContract]
public class Nullable<T> where T : struct
{
public Nullable()
{
}
internal T value;
[DataMember]
public bool HasValue { get; set; }
[DataMember]
public T Value
{
get
{
if (!this.HasValue)
throw new Exception("Property have no value");
return this.value;
}
set
{
this.value = value;
this.HasValue = true;
}
}
public Nullable(T value)
{
Value = value;
}
public T GetValueOrDefault()
{
return this.value;
}
public T GetValueOrDefault(T defaultValue)
{
if (!this.HasValue)
return defaultValue;
return this.value;
}
public override bool Equals(object other)
{
if (!this.HasValue)
return other == null;
if (other == null)
return false;
return this.value.Equals(other);
}
public override int GetHashCode()
{
if (!this.HasValue)
return 0;
return this.Value.GetHashCode();
}
public override string ToString()
{
if (!this.HasValue)
return "";
return this.Value.ToString();
}
}
调用上述函数时,我遇到以下问题:
托管调试助手“FatalExecutionEngineError”检测到
“C:\Program Files(x86)\IIS Express\iisexpress.exe”中存在问题
其他信息:运行时遇到致命错误。这个
错误地址位于线程0x2568上的0x5bd1399e。错误
代码为0xc0000005。此错误可能是CLR或中的错误
用户代码的不安全或不可验证部分。这方面的共同来源
错误包括COM互操作或PInvoke的用户封送错误
可能会损坏堆栈
感谢您的帮助
我正在使用BasicHttpBinding
<service name="MyService">
<endpoint
address=""
binding="basicHttpBinding"
name="BasicHttpEndpoint"
bindingConfiguration=""
contract="IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
编辑:
[ServiceContract]
[XmlSerializerFormat]
public interface IService1
{
[OperationContract]
List<Nullable<DateTime>> NullTest();
}
public class Service1 : IService1
{
public List<Nullable<DateTime>> NullTest()
{
return new List<Nullable<DateTime>>()
{
new Nullable<DateTime>(DateTime.Now),
new Nullable<DateTime>(DateTime.Now.AddDays(2))
};
}
}
class Program
{
static void Main(string[] args)
{
try
{
Service1Client client = new Service1Client();
ArrayOfDateTime result = client.NullTest();
foreach (DateTime dt in result)
Console.WriteLine(dt);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
List<Nullable<DateTime>> NullTest();
}
[DataContract]
public class Nullable<T> where T : struct
{
public Nullable()
{
}
internal T value;
[DataMember]
public bool HasValue { get; set; }
[DataMember]
public T Value
{
get
{
if (!this.HasValue)
throw new Exception("Property have no value");
return this.value;
}
set
{
this.value = value;
this.HasValue = true;
}
}
public Nullable(T value)
{
Value = value;
}
public T GetValueOrDefault()
{
return this.value;
}
public T GetValueOrDefault(T defaultValue)
{
if (!this.HasValue)
return defaultValue;
return this.value;
}
public override bool Equals(object other)
{
if (!this.HasValue)
return other == null;
if (other == null)
return false;
return this.value.Equals(other);
}
public override int GetHashCode()
{
if (!this.HasValue)
return 0;
return this.Value.GetHashCode();
}
public override string ToString()
{
if (!this.HasValue)
return "";
return this.Value.ToString();
}
}
public class Service1 : IService1
{
public List<Nullable<DateTime>> NullTest()
{
return new List<Nullable<DateTime>>()
{
new Nullable<DateTime>(DateTime.Now),
new Nullable<DateTime>(DateTime.Now.AddDays(2))
};
}
}
class Program
{
static void Main(string[] args)
{
Service1Client client = new Service1Client();
NullableOfdateTime[] result = client.NullTest();
foreach (NullableOfdateTime ndt in result)
Console.WriteLine(ndt.Value);
Console.ReadLine();
}
}
只要只使用可空
,它就可以使用[XmlSerializerFormat]
,而不是您的可空
实现。因此,DataContractSerializer
正在为您的Nullable
实现传递消息,但XmlSerializer
没有
换句话说,您有两种选择:
1) 使用DataContractSerializer
+您的Nullable
实现
2) 使用XmlSerializer
+Nullable
iSeries设备:
[ServiceContract]
[XmlSerializerFormat]
public interface IService1
{
[OperationContract]
List<Nullable<DateTime>> NullTest();
}
public class Service1 : IService1
{
public List<Nullable<DateTime>> NullTest()
{
return new List<Nullable<DateTime>>()
{
new Nullable<DateTime>(DateTime.Now),
new Nullable<DateTime>(DateTime.Now.AddDays(2))
};
}
}
class Program
{
static void Main(string[] args)
{
try
{
Service1Client client = new Service1Client();
ArrayOfDateTime result = client.NullTest();
foreach (DateTime dt in result)
Console.WriteLine(dt);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
List<Nullable<DateTime>> NullTest();
}
[DataContract]
public class Nullable<T> where T : struct
{
public Nullable()
{
}
internal T value;
[DataMember]
public bool HasValue { get; set; }
[DataMember]
public T Value
{
get
{
if (!this.HasValue)
throw new Exception("Property have no value");
return this.value;
}
set
{
this.value = value;
this.HasValue = true;
}
}
public Nullable(T value)
{
Value = value;
}
public T GetValueOrDefault()
{
return this.value;
}
public T GetValueOrDefault(T defaultValue)
{
if (!this.HasValue)
return defaultValue;
return this.value;
}
public override bool Equals(object other)
{
if (!this.HasValue)
return other == null;
if (other == null)
return false;
return this.value.Equals(other);
}
public override int GetHashCode()
{
if (!this.HasValue)
return 0;
return this.Value.GetHashCode();
}
public override string ToString()
{
if (!this.HasValue)
return "";
return this.Value.ToString();
}
}
public class Service1 : IService1
{
public List<Nullable<DateTime>> NullTest()
{
return new List<Nullable<DateTime>>()
{
new Nullable<DateTime>(DateTime.Now),
new Nullable<DateTime>(DateTime.Now.AddDays(2))
};
}
}
class Program
{
static void Main(string[] args)
{
Service1Client client = new Service1Client();
NullableOfdateTime[] result = client.NullTest();
foreach (NullableOfdateTime ndt in result)
Console.WriteLine(ndt.Value);
Console.ReadLine();
}
}
只要您使用
[DataContract]
和[DataMember]
正确地装饰您的可为空的,它就可以工作
iSeries设备:
[ServiceContract]
[XmlSerializerFormat]
public interface IService1
{
[OperationContract]
List<Nullable<DateTime>> NullTest();
}
public class Service1 : IService1
{
public List<Nullable<DateTime>> NullTest()
{
return new List<Nullable<DateTime>>()
{
new Nullable<DateTime>(DateTime.Now),
new Nullable<DateTime>(DateTime.Now.AddDays(2))
};
}
}
class Program
{
static void Main(string[] args)
{
try
{
Service1Client client = new Service1Client();
ArrayOfDateTime result = client.NullTest();
foreach (DateTime dt in result)
Console.WriteLine(dt);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
List<Nullable<DateTime>> NullTest();
}
[DataContract]
public class Nullable<T> where T : struct
{
public Nullable()
{
}
internal T value;
[DataMember]
public bool HasValue { get; set; }
[DataMember]
public T Value
{
get
{
if (!this.HasValue)
throw new Exception("Property have no value");
return this.value;
}
set
{
this.value = value;
this.HasValue = true;
}
}
public Nullable(T value)
{
Value = value;
}
public T GetValueOrDefault()
{
return this.value;
}
public T GetValueOrDefault(T defaultValue)
{
if (!this.HasValue)
return defaultValue;
return this.value;
}
public override bool Equals(object other)
{
if (!this.HasValue)
return other == null;
if (other == null)
return false;
return this.value.Equals(other);
}
public override int GetHashCode()
{
if (!this.HasValue)
return 0;
return this.Value.GetHashCode();
}
public override string ToString()
{
if (!this.HasValue)
return "";
return this.Value.ToString();
}
}
public class Service1 : IService1
{
public List<Nullable<DateTime>> NullTest()
{
return new List<Nullable<DateTime>>()
{
new Nullable<DateTime>(DateTime.Now),
new Nullable<DateTime>(DateTime.Now.AddDays(2))
};
}
}
class Program
{
static void Main(string[] args)
{
Service1Client client = new Service1Client();
NullableOfdateTime[] result = client.NullTest();
foreach (NullableOfdateTime ndt in result)
Console.WriteLine(ndt.Value);
Console.ReadLine();
}
}
System.Nullabe
是一个struct
,而不是一个类。当您可以将您类型的变量分配给null
时,使用HasValue
没有多大意义。但是cshtml5
支持null
我也尝试过,请注意,我在服务中有[XmlSerializerFormat]
属性。请参阅我的编辑。