我遇到了 WCF 服务正常工作的问题。我正在尝试创建一个双工服务,但每当我尝试返回自定义数据类型时,它都会破坏客户端。
如果我删除 GetTestData 一切正常。当我添加它时,客户端的一切都会中断。似乎我可以通过取消选中“在引用的程序集中重用类型”来修复它,但我不确定这样做是否有负面影响。
这是代码
ITestService.cs
namespace MyApp.Server
{
[ServiceContract(Namespace = "http://www.mysite.net", CallbackContract = typeof(IDuplexTestClient))]
public interface ITestService
{
[OperationContract]
string Hello();
[OperationContract]
TestData GetTestData();
}
public interface IDuplexTestClient
{
[OperationContract(IsOneWay = true)]
void TestCallback(string message);
}
}
测试服务.cs
namespace MyApp.Server
{
[ServiceBehavior(Namespace = "http://www.mysite.net")]
public class TestService : ITestService, ICrossDomainPolicyResponder
{
public string Hello()
{
return "Hello";
}
public TestData GetTestData()
{
return new TestData();
}
#region ICrossDomainPolicyResponder Members
public Stream GetSilverlightPolicy()
{
string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers=""*"">
<domain uri=""*""/>
</allow-from>
<grant-to>
<resource path=""/"" include-subpaths=""true""/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>";
return StringToStream(result);
}
public Stream GetFlashPolicy()
{
string result = @"<?xml version=""1.0""?>
<!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
<cross-domain-policy>
<allow-access-from domain=""*"" />
</cross-domain-policy>";
return StringToStream(result);
}
private Stream StringToStream(string result)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
return new MemoryStream(Encoding.UTF8.GetBytes(result));
}
#endregion
}
}
测试数据.cs
namespace MyApp.SDK
{
[DataContract(Namespace = "http://www.mysite.net")]
public class TestData
{
[DataMember]
public string TestString { get; set; }
}
}