2013-06-26 73 views
0

最近我一直在學習很多關於MVP設計模式的知識。我已經完成了整個解決方案,直到我開始在表示層上使用WCF服務。請在下面找到我遇到的問題的詳細信息。使用MVP設計模式的WCF服務問題

我在Windows窗體模型項目中使用服務引用。 Windows窗體應用程序項目引用Model,View,Presenter項目。

我遇到例外情況是:

Could not find default endpoint element that references contract 'ActionService.IActionService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. 

在我的演示類的公共構造,看起來像發生異常:

public class Presenter<T> where T : IView 
{ 
    /// <summary> 
    /// Gets and sets the model statically. 
    /// </summary> 
    protected static IModel Model { get; private set; } 

    /// <summary> 
    /// Static constructor 
    /// </summary> 
    static Presenter() 
    { 
     Model = new Model(); 
    } 

    /// <summary> 
    /// Constructor. Sets the view. 
    /// </summary> 
    /// <param name="view">The view.</param> 
    public Presenter(T view) 
    { 
     View = view; 
    } 

    /// <summary> 
    /// Gets and sets the view. 
    /// </summary> 
    public T View { get; private set; } 
} 

這裏是我的具體演示代碼class(AppConfigPresenter):

public class AppConfigPresenter : Presenter<IApplicationConfigurationView> 
{ 
    public AppConfigPresenter(IApplicationConfigurationView view) 
     : base(view) 
    { 

    } 

    /// <summary> 
    /// Displays the application configurations on the form 
    /// </summary> 
    /// <param name="applicationId"></param> 
    /// <param name="machineName"></param> 
    public void Display(byte applicationId, string machineName) 
    { 
     View.ApplicationConfigurations = Model.GetApplicationConfigurations(applicationId, machineName); 
    } 
} 

在windows窗體應用程序中,這裏是調用代碼:

public partial class FormMain : Form, IApplicationConfigurationView 
{ 
    private AppConfigPresenter _appConfigPresenter; 

    public FormMain() 
    { 
     InitializeComponent(); 
     _appConfigPresenter = new AppConfigPresenter(this); 
    } 

    public IList<ApplicationConfigurationModel> ApplicationConfigurations 
    { 
     set 
     { 
      var applicationConfigurations = value; 

      BindApplicationConfigurations(applicationConfigurations); 
     } 
    } 

    private void BindApplicationConfigurations(IList<ApplicationConfigurationModel> applicationConfigurations) 
    { 
     foreach(var config in applicationConfigurations) 
     { 
      listBox1.Items.Add(config.ConfigKey); 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     _appConfigPresenter.Display(7, "xxxxxxx"); 
    } 
} 

,這裏是我的web.config位於我的主機層(這是在.SVC所在的層)。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <services> 
     <service behaviorConfiguration="AppConfigBehavior" name="FileManipulationServiceLayer.ServiceImplementation.AppConfigService"> 
      <endpoint address="" binding="wsHttpBinding" contract="FileManipulationServiceLayer.ServiceContracts.IActionService"> 
      <identity> 
       <dns value="localhost"/> 
      </identity> 
      </endpoint> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
     </services> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="AppConfigBehavior"> 
        <serviceMetadata httpGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="false" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
</configuration> 

這是我的app.config那是我Windows窗體示範工程:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <wsHttpBinding> 
       <binding name="WSHttpBinding_IActionService" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
        allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" /> 
        </security> 
       </binding> 
      </wsHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:51516/ActionService.svc" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IActionService" 
       contract="ActionService.IActionService" 
       name="WSHttpBinding_IActionService"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
     </client> 
    </system.serviceModel> 
</configuration> 

編輯:

這裏是我的服務聯繫人:

namespace FileManipulationServiceLayer.ServiceContracts 
{ 
    /// <summary> 
    /// IService is the interface for Patterns in Action public services. 
    /// </summary> 
    /// <remarks> 
    /// Application Facade Pattern. 
    /// </remarks> 
    [ServiceContract(SessionMode = SessionMode.Allowed)] 
    public interface IActionService 
    { 
     [OperationContract] 
     TokenResponse GetToken(TokenRequest request); 

     [OperationContract] 
     ApplicationConfigurationResponse GetApplicationConfigurations(ApplicationConfigurationRequest request); 
    } 
} 

這裏是我的.svc文件:

<%@ ServiceHost Language="C#" Debug="true" Service="FileManipulationServiceLayer.ServiceImplementation.AppConfigService" %> 

我的新App.config文件:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <wsHttpBinding> 
       <binding name="WSHttpBinding_IActionService" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
        allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" /> 
        </security> 
       </binding> 
      </wsHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:51516/ActionService.svc" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IActionService" 
       contract="FileManipulationServiceLayer.ServiceImplementation.AppConfigService" name="WSHttpBinding_IActionService"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
     </client> 
    </system.serviceModel> 
</configuration> 

如前面提到的,我仍然得到同樣的錯誤。

新的編輯:

每當我更新服務的參考,它增加了更多的的app.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <wsHttpBinding> 
       <binding name="WSHttpBinding_IActionService" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
        allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" /> 
        </security> 
       </binding> 
       <binding name="WSHttpBinding_IActionService1" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
        allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" /> 
        </security> 
       </binding> 
      </wsHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:51516/ActionService.svc" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IActionService" 
       contract="FileManipulationServiceLayer.ServiceImplementation.AppConfigService" 
       name="WSHttpBinding_IActionService"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
      <endpoint address="http://localhost:51516/ActionService.svc" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IActionService1" 
       contract="ActionService.IActionService" name="WSHttpBinding_IActionService1"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
     </client> 
    </system.serviceModel> 
</configuration> 

回答

0

合同名稱必須相同(包括命名空間)在configs和服務合同。它看起來你有一個不匹配:* FileManipulationServiceLayer.ServiceContracts.IActionService 'ActionService.IActionService'*

+0

我添加了一些編輯,但我仍然得到像以前一樣的錯誤。還有什麼我可以給你們一些想法? –

+0

App.config中的端點名稱是否需要與合同名稱相同? –

+0

不,但我認爲它必須與.svc中的名稱相同 –