我试图在WCF中实现工厂模式,但它不起作用。我创建了一个名为TestServiceLibrary
的WCFServiceLibrary项目。然后我创建了名为EmployeeService
的wcf服务,它具有IEmployeeService
接口。我在ViewAttendance
类中定义了SaveAttendance
,IEmployeeService
方法,并在EmployeeService
类中实现。然后,我创建了一个客户端Web应用程序,它通过创建名为“EmployeeServiceRef”的服务引用来使用EmployeeService
,并在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();
时,它会调用ViewAttendance()
EmployeeService
而不是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>
new EmployeeCardReaderServiceRef.EmployeeServiceClient();
应该读
new EmployeeCardReaderServiceRef.EmployeeCardReaderServiceClient();
现在的方式是,您通过两个引用调用相同的服务。