0

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

1 回答 1

0

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

于 2013-06-26T15:59:10.493 回答