Question

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; }
    }
}
Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top