0

I am having an issue with a WCF service working correctly. I am trying to create a duplex service but evertime I try to return a custom datatype it breaks the client side.

If I remove GetTestData everything works fine. When I add it everything breaks on the client side. It seems like I can fix it by unchecking "Reuse types in referenced assemblies", but I am not sure if there are negative side effects in doing this.

Here is the code

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; }
    }
}
thecaptain0220
  • 2,098
  • 5
  • 30
  • 51
  • If I allow turn off reuse types then I get two copies of TestData because I need to use it in the client. So I have MyApp.TestServiceReference.TestData and MyApp.SDK.TestData. Knowing that these will be identical is there any easy way for me to cast the service reference type to the local SDK type? – thecaptain0220 Apr 20 '12 at 18:32

2 Answers2

0

Your type TestData is declared on the server side.

For the client to use the service that returns TestData it must know what TestData looks like.

This is set up when you do an add service reference.

There are two ways it can be done.

  • The client has a reference to the dll containing the TestData
  • The Add Service Reference generates types on the client

The "Reuse types in referenced assemblies" says that you want to use the first option.

There is no "right" way to do it. If you have control over the client and server you can reuse dll's and therefore not need to update service references when types change.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • The server is WPF and the client is Silverlight. I think this is the issue. They actually both have TestData but they are separate dlls because one is compiled for Silverlight and one for WPF. Is there any way to share it between the two in this case? – thecaptain0220 Apr 19 '12 at 21:34
  • If they were exactly the same it would work. You probably have different namespaces. In VS you can add an existing project to a solution – Shiraz Bhaiji Apr 19 '12 at 21:46
  • Thats why I am confused. It is the same class in the MyApp.SDK namespace. The Silverlight and WPF solutions just have different projects pointing to the same files. – thecaptain0220 Apr 20 '12 at 01:15
  • There is somthing weird going on. I have the TestData class in a Silverlight class library. The file is added to my server as a link and the library is referenced by my client. I add the function returning TestData, update service references, and it starts saying "The type or namespace name 'TestServiceClient' does not exist in the namespace 'MyApp.Client.TestServiceReference' (are you missing an assembly reference?)" – thecaptain0220 Apr 20 '12 at 14:59
  • I think this has do with the way I am trying to do the polling duplex service with Silverlight 5. – thecaptain0220 Apr 20 '12 at 15:12
0

I'm still not 100% sure what is going on here but I found a solution that works for me. I am able to use the "Reuse types in specified referenced assemblies" to get the functionality I need. This fixed the problem but still allows me to share the MyApp.SDK classes between the client and server.

thecaptain0220
  • 2,098
  • 5
  • 30
  • 51