拜托,帮帮我! 我的项目存在一些问题(使用WCF的WPF)。 我的项目是客户端 - 服务器交互。在服务器I中,EF与PatternRepository交互。在服务器上它是我有服务的wcf交互。 每个服务其存储库。在每个服务中,我都有一组用于服务器和客户端之间通信的命令。客户端和服务器之间的数据传输通过Json进行。例如,它的服务:
public class ProductRepositoryService : IProductRepositoryService
{
public void AddProduct(string json)
{
_productRepository.Add(wrapperProduct.DeserializeProduct(json));
_productRepository.Save();
}
public void DeleteProduct(string json)
{ productRepository.Delete(_productRepository.GetById(wrapperProduct.DeserializeProduct(json).Id));
_productRepository.Save();
}
}
示例,它与ProductService交互的ProductSeviceLogics:
ProductRepositoryServiceClient _service1Client;
public ProductSeviceLogics()
{
this._service1Client = new ProductRepositoryServiceClient();
}
public void AddProduct(string json)
{
_service1Client.AddProduct(json);
}
public void DeleteProduct(string json)
{
_service1Client.DeleteProduct(json);
}
这意味着如果我要创建服务。我将为服务器和客户端上的每个服务创建这些方法。我认为这很糟糕。
所以我的问题,我怎么能这样做,这些方法将适用于所有服务? 也就是说,我不想为每个服务和每个客户端创建这种方法。
答案 0 :(得分:0)
我建议您查看这篇ASP.NET MVC Solution Architecture文章。下载代码并查看它们如何在单独的类库中维护存储库/服务/模型,并在用户界面或WCF或WebAPI中使用。
这里我将提供一些示例解决方案模式。
创建一个新的空白解决方案:文件 - >新项目 - >其他项目类型 - >空白解决方案并将其命名为MyProject。
创建下面列出的新类库
MyProject.Model
MyProject.Data
添加模型参考。
包含EF(DbSet和DbContext)和ProductRepository,SalesRepository等存储库。
MyProject.Service
创建用户界面和WCF服务
MyProject.Web
MyProject.WCF
添加模型和服务的参考。
您的工作流程如此WCF或网络电话 - >服务 - >存储库 - > EF,因此您可以避免为客户端和服务器创建多个服务。
希望这会对你有所帮助。
答案 1 :(得分:0)
我解决了这个问题。 为WCF服务生成代理
通过实现ClientBase类*
生成代理使用ClientBase类选项生成代理具有在运行时创建代理的优势,因此它将适应服务实现更改。让我们按照步骤生成代理。
将客户端项目添加到名为“ClientApp2”的解决方案中 基本上是控制台应用程序。
将StudentService Project的引用添加到ClientApp2。添加以下内容 使用ClientBase的代理类:
public class StudentServiceProxy : ClientBase<IStudentService>, IStudentService
{
public string GetStudentInfo(int studentId)
{
return base.Channel.GetStudentInfo(studentId);
}
}
注意:不要忘记将“使用StudentService”添加到课程中。
以下是ClientApp2中program.cs类的代码。我们使用上面创建的代理类与WCF服务进行通信 “StudentService”。
class Program
{
static void Main(string[] args)
{
StudentServiceProxy myclient;
myclient = new StudentServiceProxy();
int studentId = 1;
Console.WriteLine(“Calling StudentService with StudentId =1…..”);
Console.WriteLine(“Student Name = {0}”, myclient.GetStudentInfo(studentId));
Console.ReadLine();
}
}
注意:不要忘记将“using System.ServiceModel”添加到类。
App.Config file will have following configuration:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name=”WSHttpBinding_IStudentService” />
</wsHttpBinding>
</bindings>
<client>
<endpoint address=”http://localhost:4321/StudentService”
binding=”wsHttpBinding”
bindingConfiguration=”WSHttpBinding_IStudentService”
contract=”StudentService.IStudentService”
name=”WSHttpBinding_IStudentService”>
</endpoint>
</client>
现在,当我们运行客户端应用程序时,我们将收到与之前的“添加服务引用”选项中相同的输出。