次の方法で共有


How to: Create an Object Model

 

Applies To: Windows Server 2012 Essentials, Windows Home Server 2011, Windows Storage Server 2008 R2 Essentials, Windows Small Business Server 2011 Essentials

The object model defines the operations that are used to access the functionality of the provider. For the chat application, the object model provides methods to connect to the server and to send and receive text. The object model consists of two parts: the public-facing operations, and the backend operations.

Note

Before you start performing the steps in the following procedures, you should make sure that you have copied the template in the SDK to the ProjectTemplates folder for Visual Studio 2010.

In this section, you complete the following tasks:

  • Create the class that contains the public operations of the object model.

  • Create the class that contains the backend operations of the object model.

  • Create the service contract for the object model.

  • Create the callback contract for the object model.

To create the class that contains the public operations of the object model

  1. Open Visual Studio 2010 as an administrator by right-clicking the program in the Start menu and selecting Run as administrator.

  2. Click File, click New, and then click Project. In the New Project dialog box, click Visual C#, and then select the WSS Provider Service template, and name it ChatSample. Name the solution MyProvider and use the default Location. Click OK.

    Important

    Because all of the assemblies are stored in one bin folder, you should make sure that the name of your project is unique and does not match any other binaries in the folder. Suggestions for naming assemblies include the following: avoid generic names for your assemblies, such as “common.dll,” “antivirus.dll,” or “backup.dll”; consider prefixing the name of your assembly with your company name or identifier.

  3. Add a reference to the ProviderFramework.dll file that you located when you set up your development environment.

  4. Open the ObjectModel.cs file.

  5. To moderate chat, the object model must keep track of the user name of the person who is chatting. Add the following code to define a user name property:

    private string m_userName = "<none>";  
    public string UserName {  
       get { return m_userName; }  
       internal set {   
          if (m_userName != value) {  
             m_userName = value;  
             RaisePropertyChanged("UserName");  
          }  
       }  
    }  
    
  6. Change the Connect method to include the user name that was defined in the previous step.

    public void Connect(string userName) {  
       m_userName = userName;  
       m_backend.Connect(userName);  
    }  
    
  7. Change the name of the PerformOperation method to SendChat and include a text parameter.

    public void SendChat(string text) {  
       m_backend.SendChat(text);  
    }  
    
  8. Change EventArgs to ChatReceivedEventArgs, and add the user and text parameters to the RaiseResponseReceived method.

    public EventHandler<ChatReceivedEventArgs> ResponseReceived;  
    
    private void RaiseResponseReceived(string user, string text) {  
       EventHandler<ChatReceivedEventArgs> changed = ResponseReceived;  
       if (changed != null)   
          changed(this, new ChatReceivedEventArgs(user, text));  
    }  
    
  9. Add the user and text parameters to the ReceiveResponse method.

    internal void ReceiveResponse(string user, string text) {  
       RaiseResponseReceived(user, text);  
    }  
    
  10. Save the ChatSample.ObjectModel project.

  11. You must define the ChatReceivedEventArgs class. Right-click the ChatSample.ObjectModel project, click Add, and then click New Item.

  12. In the Templates pane, click Class, type ChatReceivedEventArgs.cs in the Name text box, and then click Add.

  13. Ensure that the ChatReceivedEventArgs class contains the following code:

    public class ChatReceivedEventArgs : EventArgs {  
       public string Text { get; set; }  
       public string User { get; set; }  
       public ChatReceivedEventArgs(string user, string text) {  
          User = user;  
          Text = text;  
       }   
    }  
    
  14. Save the project and do not close the solution; you will use it for the next procedure.

To enable communication between the public operations and the provider, create a backend to the Object Model.

To create the class that contains the backend operations of the object model

  1. In the ChatSample.ObjectModel project, open the ObjectModelImplementation.cs file.

  2. Change the Connect method to include the user name.

    private string m_name;  
    
    public void Connect(string name) {  
       m_name = name;  
       m_connector.Connect();  
    }  
    
  3. Change the connector_ConnectionOpened method to include the user name.

    void m_connector_ConnectionOpened(  
       object sender, ProviderConnectionOpenedArgs<IProvider> e) {  
       try {  
          m_connector.Connection.SetUserName(m_name);  
          m_receiver.ConnectionCompleted();  
       }  
       catch(CommunicationException) {  
          // the closed event will do the appropriate cleanup  
       }  
       catch(TimeoutException) {  
          // the closed event will do the appropriate cleanup  
       }  
    }  
    
  4. Change the name of the PerformOperation method to SendChat and include a text parameter.

    public void SendChat(string text) {  
       try {  
          m_connector.Connection.SendChat(text);  
       }   
       catch (CommunicationException) {  
          // the handler will disconnect  
       }  
       catch (TimeoutException) {  
          // Timeouts in a local system generally represent an error case.   
       }  
    }  
    
  5. Change the callback to include the user name and text.

    void IProviderCallback.ResponseReceived(string user, string text) {  
       m_receiver.ReceiveResponse(user, text);  
    }  
    
  6. Save the project and do not close the solution.

The next step is to create the contracts for the object model. To do this, see How to: Create a Contract.