WCF 多接口

发布于 2024-11-16 03:40:18 字数 3731 浏览 2 评论 0原文

我真的很想了解这种 WCF 技术,似乎过去几个月的信息填塞在某种程度上扭曲了我应该如何构建客户端/服务器应用程序的总体概念。

如果有人可以在开发我的应用程序和实现具有多个接口的 Duplex WCF 服务时阐明一些最佳实践。

概要:我想开发一个应用程序,用户可以连接到服务器,然后说'..将联系人添加到 SQL 数据库。我已经发现了很多方法来做到这一点,但最终想知道当需要进一步开发应用程序时我正在走上正确的道路。

我发现的一些模型是......

客户端有自己的 LINQ to SQL 类并处理所有数据进出数据......不好。真的很慢。 Linq Select 命令的实施不佳导致 LINQ 和 SQL 连接的开销。

另一种模型是开发服务来实现用于 CRUD 操作的 linq to sql 命令,但这仍然不向连接到该服务的其他客户端提供实时数据更新。

因此,我制作了一个基本应用程序,当客户端登录到服务时,回调通道将添加到回调列表中。当客户端向服务提供新联系人时,它会使用新联系人调用对所有通道客户端的回调,并且客户端功能负责将联系人添加到正确的位置。

所以现在我想实现一个 User 对象,也许还有 2 个其他业务对象,比如 Project 和 Item,比如 Item...我的想法是像这样创建我的服务

[Serializable]
[DataContract]
[ServiceBehavior(
   ConcurrencyMode = ConcurrencyMode.Single,
   InstanceContextMode = InstanceContextMode.PerCall)]
public class Project: IProject
       {
    [DataMember()]
    public int projectID;
          public int Insert(objSubItem _objSubItem)
    {
        // code here
               }

等等,

 [ServiceContract(
    Name = "Project",
    Namespace = "",
    SessionMode = SessionMode.Required,
    CallbackContract = typeof(IProjectCallback))]
public interface IProject
{
    /// <summary>
    /// Inserting a Project record to the database
    /// </summary>
    /// <param name="_project">Project from Client</param>
    /// <return>ProjectID back to the client if -1 then fail</return>
     [OperationContract()]
     int Insert(Project _project);

显然

    public interface IProjectCallback
{
     /// <summary>
    /// Notifies the clients that a Project has been added
    /// </summary>
    /// <param name="_project">Inserted Project</param>
    [OperationContract(IsOneWay = true)]
    void NotifyProjectInserted(Project _project);
}

我有其他 CRUD 功能和功能来确保客户端和服务器数据记录在编辑时都是只读的。

现在,如果我有多个对象,最好的布局方式是什么?

我正在考虑创建一个 servce.cs 和一个 Iservice.cs 以及一个 IserviceCallback 来协商客户端通道数量。我还使用服务的部分类来实现 Iproject 和 IUser 来正确调用服务回调以及调用对象插入。

我会这样做吗

 [ServiceContract(Name = "Service",
 Namespace = "", 
 SessionMode = SessionMode.Required, 
 CallbackContract = typeof(IServiceCallBack))]
 [ServiceKnownType(typeof(Project))]
 [ServiceKnownType(typeof(User))]
 public interface IService
 {

     // code here
 }

    [ServiceBehavior(
    ConcurrencyMode = ConcurrencyMode.Single, 
    InstanceContextMode = InstanceContextMode.PerCall)]
    public partial class Service :  IUser
    {

    public int Insert(User _User)
    {
       //       
    }
    }
    public partial class Service : IProject
    {

    public int Insert(Project _project)
    {
       // code here
    }
    }
    public partial class Service : IService
   {

    //   functions here 

    }
    }

如果感觉这种方法对于一个界面来说是正确的,但感觉我需要一些“最佳实践”帮助。

预先非常感谢,

克里斯·利奇

嗨理查德, 我很欣赏你的回复。正如您所看到的,这是我在任何与编程相关的论坛上发表的第一篇文章,也是第三篇文章。正如我的谷歌自动填充历史所示,我的编程生活与谷歌非常接近,但现在是时候开始问我自己的问题了,所以我感谢您迄今为止的帮助。我真的很想了解如何最好地管理分布式客户端/服务应用程序之间的数据一致性的总体方法。我正在研究 Telerik ORM 和实体框架作为解决方案,并通过 WCF 服务公开实体,但我缺乏在客户端之间实现数据一致性的理解。我已经成功开发了一个 netDualTcp 聊天应用程序,并使用了客户端回调上下文列表来发送加入/离开和聊天功能。我缺乏整体情况,但似乎如果我的 sql 数据库中的所有表都有一个内存(静态)版本,并且如果可能的话,或者让客户端直接绑定到这些列表,或者这似乎最适合我的自定义用户控件来处理连接,以便服务器知道谁打开了该特定用户控件,并可以将更改定向到注册到回调合约的那些客户端。这样,客户就不必每次打开应用程序时都加载整个项目。我正在考虑一种多用途应用程序,例如联系人/拨款申请程序,其中用户将使用应用程序的不同部分,并且并不总是需要一次访问所有信息。当用户第一次登录时,我希望该服务将为客户端附加一个回调合同,并且在身份验证时将一些信息加载回客户端,例如基本状态,即如果他们是管理员,他们会收到通知等。他们登录后会看到一个空白画布,然后开始将自定义用户控件加载到对接面板类型界面中。我想这就是我对如何最好地管理并发性和一致性,同时最大限度地减少到客户端的负载/数据传输时间并释放两个客户端上的CPU处理时间的问题感到有点困惑的地方。我知道在编程中有多种方法可以做到这一点,但我想从这个论坛上的人们那里知道他们认为实现这种类型的灵魂的最佳方法是什么。我理解这是一个深刻的话题,但我觉得我已经走到了这一步,并且将不胜感激。再次感谢

i am really wanting to get my head around this WCF technology and it seems the last months of information cramming has somewhat distorted my overall concept of how i should build my client/server application.

If someone out there could shed some light on the best practises when developing my app and implementing a Duplex WCF service with multiple interfaces.

General outline: I am wanting to develop an app where users connect to a server and lets say'.. add contacts to an sql database. I have discovered many ways of doing this but would ultimatly like to know im heading down the right path when it comes time to developing the app further.

Some models i have discovered are...

Client has its own LINQ to SQL classes and handles all data to and from data.... BAD. really slow. overheads with LINQ and SQL connections amongst poor implementation of Linq Select command.

Another model was the develop the service to implement the linq to sql commands which are used for CRUD operations however this still doesnt provide live data updates to other clients connected to the service.

So i made a basic app that when a client logs in the to the service there Callback Channel gets added to the Callback List. When a client feeds in a new contact to the service, it invokes a callback to all channel clients with the new contact and the client side function takes care of adding the contact to the right spot.

So now i want to implement a User object and perhaps 2 more other business objects say Project and Item and lets say Item... my idea is to create my service like this

[Serializable]
[DataContract]
[ServiceBehavior(
   ConcurrencyMode = ConcurrencyMode.Single,
   InstanceContextMode = InstanceContextMode.PerCall)]
public class Project: IProject
       {
    [DataMember()]
    public int projectID;
          public int Insert(objSubItem _objSubItem)
    {
        // code here
               }

etc and

 [ServiceContract(
    Name = "Project",
    Namespace = "",
    SessionMode = SessionMode.Required,
    CallbackContract = typeof(IProjectCallback))]
public interface IProject
{
    /// <summary>
    /// Inserting a Project record to the database
    /// </summary>
    /// <param name="_project">Project from Client</param>
    /// <return>ProjectID back to the client if -1 then fail</return>
     [OperationContract()]
     int Insert(Project _project);

and

    public interface IProjectCallback
{
     /// <summary>
    /// Notifies the clients that a Project has been added
    /// </summary>
    /// <param name="_project">Inserted Project</param>
    [OperationContract(IsOneWay = true)]
    void NotifyProjectInserted(Project _project);
}

obviously i have other crud functions and functions to ensure that both client and server data records are read only when being editited.

now if i have multi objects what is it the best way to lay it out.

Im thinking to create a servce.cs and an Iservice.cs and an IserviceCallback to negotiate the client channel population.. sould i also use partial classes of the service to implement the Iproject and IUser to properly ivoke the service callbacks aswell as invoking the objects insert.

would i do it like this

 [ServiceContract(Name = "Service",
 Namespace = "", 
 SessionMode = SessionMode.Required, 
 CallbackContract = typeof(IServiceCallBack))]
 [ServiceKnownType(typeof(Project))]
 [ServiceKnownType(typeof(User))]
 public interface IService
 {

     // code here
 }

and also

    [ServiceBehavior(
    ConcurrencyMode = ConcurrencyMode.Single, 
    InstanceContextMode = InstanceContextMode.PerCall)]
    public partial class Service :  IUser
    {

    public int Insert(User _User)
    {
       //       
    }
    }
    public partial class Service : IProject
    {

    public int Insert(Project _project)
    {
       // code here
    }
    }
    public partial class Service : IService
   {

    //   functions here 

    }
    }

if feels as though the approach feels right if it was for one interface but feel that i need some "Best Practice" assistance.

Many thanks in advance,,

Chris Leach

Hi Richard,
I appreciate your response. As you see this is my first post and third ever on any forum related to programming. I have lived my programming life very close to google as shown by my google autofill history but its time to start asking questions of my own so i thank-you for your assistance so far. I am really wanting to understand an overall approach to how best managing data consistency amongst a distributed client/service application. I am looking into Telerik ORM and also Entity Framework as a solution and exposing the entities through a WCF service but i lack the understanding to implement data consistency amongst the clients. i have managed to develop a netDualTcp chat application and have used a list of client callback context to send join/leave and chat functions. I lack the overall picture however it seems that if i have a in memory (static) version of all of the tables in my sql database and either have the clients bind directly to these lists if this is possible or it seems best for my custom user controls to handle the connections so the server is aware of who has that particular user control open and can direct changes to those clients who are registered to the callback contract. that way the clients arent having to load the entire project every time they wish to open the application. I am thinking of a multi purpose application such as a contact/grant application program where users will be using different parts of the application and do not always need to access all of the information at one time. When the user first logs in i am hoping that the service will attach a callback contract for the client and several bits of information are loaded back to the client on authentaction such as a basic state i.e if they are an admin they get notifications etc. once they are logged in they are presented with a blank canvas but then begin to load custom user controls into a docking panel type interface. i guess this is where i become a little stuck about how to best manage concurrency and consistency whilst minimizing load/data transfer times to the client and freeing up cpu proccess time on both client. I know in programming there are multiple ways of doing this but i would like to know from the people on this forum what they feel the best approach to this type of soultion is. I understand its a deep topic but i feel i have come this far and a guiding hand would be appreciated. Thanks again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燕归巢 2024-11-23 03:40:18

一般来说,我发现以非抽象的方式看待服务可以让我找到正确的地方。我的服务的消费者需要做什么?

显然,我的业务层使用内部域对象来创建和操作数据。但是,业务层执行操作的方式不一定是为我的服务划分功能的最佳方式。

例如,如果任何项目中应该至少有一名用户,那么当您创建项目时,您应该同时发送至少一名用户。服务操作需要封装执行自包含业务事务所需的所有数据。

同样,许多分布式系统的丧钟是延迟——它们需要大量的往返才能完成某件事。例如,您希望能够将用户添加到项目中;实际上,您可能希望将许多用户添加到项目中。因此,您应该对操作进行建模以接受一组用户,而不是必须多次调用的单个用户。

因此,项目服务应该允许您通过服务契约执行与一个或多个项目相关的所有操作。如果用户可以独立于项目生活,那么也有用户服务。如果他们不能,那么就没有用户服务,因为一切都需要以项目为中心。

业务事务通常不仅仅是对域实体进行直接的 CRUD 操作,服务应该对它们进行建模,而不是反映数据模型

Generally I find taking a non-abstract view of a service gets me to the right place. What is it that consumers of my service are going to need to do?

I obviously have internal domain objects that are used by my business layer to create and manipulate the data. However, the way the business layer does tings isn;t necessarily the best way to partition functionality for my service.

So for example, if any project should have at least one user in it then when you create the project you should send over at least one user at the same time. The service operations need to encapsulate all of the data required to carry out a self contained business transaction.

Similarly, the death knell of many distributed systems is latency - they require lots of round trips to complete something. So, for example, you want to be able to add a user to a project; in reality you probably want to add a number of users to as project. Therefore, you should model the operation to accept a list of users not a single one which must be invoked multiple times

So a project service should allow you to do all the things related to a project, or projects, through a service contract. If users can live independently of projects then also have a user service. If they cannot then don;t have a user service as everything needs to be project focussed.

Business transactions are often more than straight forward CRUD operations on domain entities and the service should model them rather than reflecting the data model

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文