Search code examples
c#wpfwcfsilverlightmef

Silverlight MEF with WCF service


I have a dashboard project with many user controls. This week I've created an application which is more than just a user control and integrating it into my dashboard application seemed like a pain. So I searched for solutions and found MEF and PRISM. MEF seemed a bit easier than PRISM and I started to do a Hello World MEF app with this tutorial. It went well and I injected a Hello World xap successfully.

After that I tried to inject my real application and encountered some problems. I want to point out problems that I solved because I might solved them in a wrong way or they may be the reason of my current problem.

Note: My application uses a Silverlight enabled WCF Web Service to retrieve data.

First Problem

ServiceReferences.ClientConfig was not found in xap package. I added this file as link to my MEF project client side. Problem solved.

Second Problem

I am using a Settings.xml on my client side which holds the endpoints like:

<?xml version="1.0" encoding="utf-8" ?>
 <Services>
  <Service Name="MyService">
    <HostPath Name="/ClientBin/MyComponent.xap">
      <Endpoint Url="/MyService.svc"></Endpoint>
    </HostPath>
    <HostPath Name="MyComponent.Web/ClientBin/MyComponent.xap">
      <Endpoint Url="MyComponent.Web/MyService.svc"></Endpoint>
    </HostPath>
  </Service>
</Services>

and read this to get WCF Web Service service client with my 2 functions which are:

public MyServiceClient GetMyServiceClient()
    {
        if (serviceClient == null)
        {
            serviceClient = new MyServiceClient();
            Uri uriEndpointAddress = serviceClient.Endpoint.Address.Uri;
            UriBuilder ub = new UriBuilder(uriEndpointAddress)
            {
                Host = Application.Current.Host.Source.Host,
                Path =
                    GetURLForService("MyService",
                                     Application.Current.Host.Source.AbsolutePath)
            };
            serviceClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(ub.Uri);
        }
        return serviceClient;
    }

private string GetURLForService(string ServiceName, string HostURL)
    {
        string retval = "";
        XDocument doc = XDocument.Load("Settings.xml");
        if (doc.Root != null)
        {
            XElement elmService = doc.Root.Elements("Service").FirstOrDefault(c =>
            {
                XAttribute xAttribute = c.Attribute("Name");
                return xAttribute != null && xAttribute.Value.ToLower() == ServiceName.ToLower();
            });
            if (elmService != null)
            {
                XElement elmHostPath = elmService.Elements("HostPath").FirstOrDefault(c =>
                {
                    XAttribute xAttribute = c.Attribute("Name");
                    return xAttribute != null && xAttribute.Value.ToLower() == HostURL.ToLower();
                });
                if (elmHostPath != null)
                {
                    retval = elmHostPath.Element("Endpoint").Attribute("Url").Value;
                }
            }
        }

        return retval;
    }

I've added my Settings.xml file as link also and problem solved.

Main Problem

After solving these two problems, I've encountered main problem. The remote server returned an error: NotFound.

I even tried this in my Settings.xml:

<HostPath Name="/MEFHubApp/ClientBin/MyComponent.xap">
  <Endpoint Url="/MyComponent.Web/MyService.svc"></Endpoint>
</HostPath>

My MEF app can't find/use my web service no matter what I try.

Thanks


Solution

  • I found the solution for my problem. So here it is if anyone encounters the same:

    Instead of my GetMyServiceClient() from settings.xml. I initialized my service client like this:

    MyServiceClient client = new MyServiceClient("MyService_CustomBinding");
    

    Parameter is my binding in the ServiceReferences.ClientConfig and voila it worked like a charm!