【发布时间】:2014-02-26 11:14:46
【问题描述】:
我正在尝试在 WCF 中实现工厂模式,但它不起作用。
我创建了一个名为 TestServiceLibrary 的 WCFServiceLibrary 项目。然后我创建了名为EmployeeService 的wcf 服务,它具有IEmployeeService 接口。
我在IEmployeeService 接口中定义了方法ViewAttendance、SaveAttendance,并在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(); 时,它会调用EmployeeService 的ViewAttendance() 方法,而不是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