【问题标题】:WCF Services and Factory Design PatternWCF 服务和工厂设计模式
【发布时间】:2014-02-26 11:14:46
【问题描述】:

我正在尝试在 WCF 中实现工厂模式,但它不起作用。 我创建了一个名为 TestServiceLibrary 的 WCFServiceLibrary 项目。然后我创建了名为EmployeeService 的wcf 服务,它具有IEmployeeService 接口。 我在IEmployeeService 接口中定义了方法ViewAttendanceSaveAttendance,并在EmployeeService 类中实现。然后,我创建了一个使用 EmployeeService 的客户端 Web 应用程序,方法是创建一个名为“EmployeeServiceRef”的服务引用,并在 Default.aspx.cs 后面的代码中调用以下方法

EmployeeServiceRef.IEmployeeService obj = new EmployeeServiceRef.EmployeeServiceClient();
obj.ViewAttendance();

它显示了预期的结果。然后在TestServiceLibrary 项目中,我创建了另一个名为EmployeeCardReaderService 的类,它实现了IEmployeeService 接口,因为它也将具有ViewAttendance 方法但具有不同的实现,因此基本上创建了另一个WCF 服务而不创建新接口并配置为 web.config 中的一项新服务。然后在客户端应用程序中我创建了另一个服务引用

EmployeeCardReaderServiceRef

EmployeeCardReaderServiceRef.IEmployeeService obj2 = new EmployeeCardReaderServiceRef.EmployeeServiceClient();

当我调用obj2.ViewAttendance(); 时,它会调用EmployeeServiceViewAttendance() 方法,而不是EmployeeCardReaderService。 谁能告诉我是否可以在 wcf 服务中实现工厂设计模式?如果是这样,那么正确的方法是什么,因为它对我不起作用。我们也可以将工厂模式应用于 asmx webservices 吗?

namespace TestServiceLibrary
{
    [ServiceContract(Namespace="http://www.test.com")]
    public interface IEmployeeService
    {
        [OperationContract]
        string ViewAttendance();
        [OperationContract]
        string ViewEmployee();
        [OperationContract]
        string ViewDetails();
    }
}


namespace TestServiceLibrary
{
    public class EmployeeService : IEmployeeService
    {
        public string ShowWork()
        {
            return "";
        }

        #region IEmployeeService Members

        public string ViewAttendance()
        {
            /* TODO. Implementation specific to Device */
            return "EmployeeService Attendance";
        }
        public string ViewEmployee()
        {
            return "Employee Number 1";
        }

        #endregion

        #region IEmployeeService Members


        public string ViewDetails()
        {
            return "Employee Details are as follows";
        }

        #endregion
    }

    class EmployeeCardReaderService : IEmployeeService
    {
        #region IEmployeeService Members

        public string ViewAttendance()
        {
           /*TODO:
            Implementation specific to card reader customer
            */

            return "EmployeeCardReaderService Attendance";
        }

        public string ViewEmployee()
        {
            throw new NotImplementedException();
        }

        public string ViewDetails()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
      <service name="TestServiceLibrary.EmployeeService">
        <endpoint address="" binding="basicHttpBinding" contract="TestServiceLibrary.IEmployeeService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/TestServiceLibrary/EmployeeService/" />
          </baseAddresses>
        </host>
      </service>
      <service name="TestServiceLibrary.EmployeeCardReaderService">
        <endpoint address="" binding="basicHttpBinding" contract="TestServiceLibrary.IEmployeeService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/TestServiceLibrary/EmployeeCardReaderService/" />
          </baseAddresses>
        </host>
      </service>

【问题讨论】:

  • 工厂模式与否,“在 web.config 中配置为新服务”无法正常工作。发布该部分,我们也许可以提供帮助。
  • 我添加了更多信息

标签: c# asp.net web-services wcf


【解决方案1】:
new EmployeeCardReaderServiceRef.EmployeeServiceClient();

应该阅读

new EmployeeCardReaderServiceRef.EmployeeCardReaderServiceClient();

现在的方式是通过两个引用调用同一个服务。

【讨论】:

  • 当我调用新的 EmployeeCardReaderServiceRef 时,EmployeeCardReaderServiceClient 不在列表中。然后我有 EmployeeServiceClient、IEmployeeService、IEmployeeServiceChannel 三个选项,即使我已经配置了对新服务的引用
  • @waqas:那是因为您的 EmployeeCardReaderService 类不在 public 范围内,因此从未创建其各自的客户端引用。只需添加此 public 说明符,它应该可以按照您的预期工作:)
  • @dreamweiver:公开声明没有任何区别
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 2015-12-28
相关资源
最近更新 更多