我遇到了WCF服务正常工作的问题。我试图创建一个双工服务,但加班我试图返回一个自定义的数据类型,它打破了客户端。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);
}
}
TestService.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
}
}
TestData.cs
namespace MyApp.SDK
{
[DataContract(Namespace = "http://www.mysite.net")]
public class TestData
{
[DataMember]
public string TestString { get; set; }
}
}
如果我允许关闭重用类型,那么我得到TestData的两个副本,因为我需要在客户端使用它。所以我有MyApp.TestServiceReference.TestData和MyApp.SDK.TestData。知道这些将是相同的是我有什么简单的方法将服务引用类型转换为本地SDK类型? – thecaptain0220 2012-04-20 18:32:11