2013-05-09 117 views
0

我有一個帶有許多用戶控件的儀表板項目。本週,我創建了一個不僅僅是用戶控件而且將它集成到儀表板應用程序中的應用程序,這看起來很痛苦。於是我尋找解決方案並找到了MEF和PRISM。 MEF似乎比PRISM容易一些,我開始用this教程做一個Hello World MEF應用程序。它進行得很順利,我成功地注入了一個Hello World xap。帶WCF服務的Silverlight MEF

之後,我試圖注入我真正的應用程序,並遇到一些問題。我想指出我解決的問題,因爲我可能以錯誤的方式解決它們,或者它們可能是我當前問題的原因。

注意:我的應用程序使用啓用Silverlight的WCF Web服務來檢索數據。

第一問題

ServiceReferences.ClientConfig未在XAP包中找到。 我將這個文件作爲鏈接添加到我的MEF項目客戶端。問題解決了。

第二問題

我使用我的客戶機端將Settings.xml持有端點,如:

<?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> 

和閱讀這讓WCF Web Service的服務客戶與我的2個功能它們是:

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; 
    } 

我已經添加了我的Settings.xml文件作爲鏈接也解決了問題。

主要問題

解決這兩個問題之後,我遇到的主要問題。 遠程服務器返回錯誤:NotFound。

我甚至在我將Settings.xml試過這樣:

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

我MEF的應用程序無法找到/用我的網絡服務,無論我怎麼努力。

謝謝

+0

在IE9 +(F12)中啓動開發工具並開始捕獲網絡流量。查看正在發送的請求。如果請求構造不正確 - 那麼這就是客戶端問題 - 開始調試客戶端。如果請求看起來正確 - 那麼這是一個服務器問題 - 開始調試服務器。 「NotFound」在許多不同情況下都會返回,您需要縮小這個範圍。 – boris 2013-05-09 15:18:31

+0

只是任何人閱讀這篇文章,我不知道如何「添加爲鏈接」。這篇文章解釋得很好... http://www.global-webnet.net/blogengine/post/2009/01/03/PrismWCF-Cannot-find-ServiceReferencesClientConfig-in-xap-application-package.aspx 節選:轉到主程序集(在我們的例子中爲SDMS.Silverlight),並添加一個現有項目 - 粘貼完整的ClientConfig文件路徑,然後單擊「添加」按鈕上的向下三角形並選擇「添加鏈接」 – Nexxas 2015-07-23 15:01:52

回答

0

我找到了我的問題的解決方案。所以在這裏,如果有人遇到相同的:

而不是我的GetMyServiceClient() from settings.xml。我初始化我的服務的客戶是這樣的:

MyServiceClient client = new MyServiceClient("MyService_CustomBinding"); 

參數是我在ServiceReferences.ClientConfig結合,瞧它的工作就像一個魅力!