WCF服务使用自定义数据类型中断
我对WCF服务是否正常工作有问题。我试图创建一个双工服务,但每次我试图返回一个自定义数据类型时,它都会破坏客户端 如果我删除GetTestData,一切正常。当我添加它时,客户端的一切都会中断。似乎我可以通过取消选中“在引用的程序集中重用类型”来修复它,但我不确定这样做是否有负面的副作用 这是密码 ITestService.csWCF服务使用自定义数据类型中断,wcf,pollingduplexhttpbinding,custom-data-type,Wcf,Pollingduplexhttpbinding,Custom Data Type,我对WCF服务是否正常工作有问题。我试图创建一个双工服务,但每次我试图返回一个自定义数据类型时,它都会破坏客户端 如果我删除GetTestData,一切正常。当我添加它时,客户端的一切都会中断。似乎我可以通过取消选中“在引用的程序集中重用类型”来修复它,但我不确定这样做是否有负面的副作用 这是密码 ITestService.cs namespace MyApp.Server { [ServiceContract(Namespace = "http://www.mysite.net", C
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);
}
}
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
}
}
TestService.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);
}
}
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在服务器端声明 为了让客户端使用返回TestData的服务,它必须知道TestData是什么样子 这是在执行添加服务引用时设置的 有两种方法可以做到这一点
- 客户端有一个对包含TestData的dll的引用
- 添加服务引用在客户端上生成类型
没有“正确”的方法来做这件事。如果您可以控制客户端和服务器,则可以重用dll,因此不需要在类型更改时更新服务引用。我仍然不能100%确定这里发生了什么,但我找到了一个适合我的解决方案。我能够使用“在指定的引用程序集中重用类型”来获得所需的功能。这解决了问题,但仍然允许我在客户端和服务器之间共享MyApp.SDK类。服务器是WPF,客户端是Silverlight。我认为这就是问题所在。它们实际上都有测试数据,但它们是独立的DLL,因为一个是为Silverlight编译的,另一个是为WPF编译的。在这种情况下,有没有办法让这两个人分享呢?如果他们完全一样,那就行了。您可能有不同的名称空间。在VS中,您可以将现有项目添加到解决方案中,这就是我感到困惑的原因。它与MyApp.SDK命名空间中的类相同。Silverlight和WPF解决方案只是有不同的项目指向相同的文件。我在Silverlight类库中有TestData类。该文件作为链接添加到我的服务器,我的客户端引用该库。我添加了返回TestData、更新服务引用的函数,它开始说“类型或命名空间名称'TestServiceClient'在命名空间'MyApp.Client.TestServiceReference'中不存在”(是否缺少程序集引用?)我认为这与我尝试使用Silverlight 5执行轮询双工服务的方式有关。如果我允许关闭重用类型,那么我将获得两个TestData副本,因为我需要在客户端中使用它。所以我有MyApp.TestServiceReference.TestData和MyApp.SDK.TestData。知道这些将是相同的,有没有简单的方法可以将服务引用类型转换为本地SDK类型?