使用MVP设计模式的WCF服务问题

时间:2013-06-26 15:27:21

标签: c# wcf mvp

我最近一直在学习很多关于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.

我的Presenter类的公共构造函数中出现异常,如下所示:

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

以下是我的具体演示者类(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>

这是我的Windows窗体模型项目中的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="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>

1 个答案:

答案 0 :(得分:0)

配置和服务合同中的合同名称必须相同(包括名称空间)。它看起来你不匹配:* FileManipulationServiceLayer.ServiceContracts.IActionService vs。'ActionService.IActionService'*