Implementing a service contract for the native endpoint
To enable service access to your document type, add a class that implements each of the methods in your native endpoint interface.
Typically, the class that implements the interface is in a separate file. For example, the sample Leads service implements the ILeads interface in the LeadService.cs file.
The following steps show how to use the Dynamics GP Service framework to implement the service methods for the native endpoint:
Verify the namespace.
The class that implements the interface must be in the same namespace as the interface. Verify that your <projectname>.cs file includes your namespace or add the namespace to the file.
For example, the sample Leads service uses the SampleLeadService.Contract namespace for the native endpoint.
Add a class to the namespace.
Add a class to the namespace that implements the interface you specified.
The following code example adds the Leads class to the SampleLeadService.Contract namespace. Notice how the Leads class implements the ILeads interface.
// Implement the service contract for the native endpoint
namespace SampleLeadService.Contract { public class Leads : ILeads {
}
}
Add the ServiceBehavior attribute.
Use the properties of the ServiceBehavior attribute to specify the name and namespace of the service. Use the same namespace as the native endpoint of the Dynamics GP Service.
The following code example adds the ServiceBehavior attribute to the Leads class. Notice how the Name property specifies a name for the service and the Namespace property specifies the namespace for the native endpoint of the Dynamics GP Service.
// Implement the service contract for the native endpoint
namespace SampleLeadService.Contract { [ServiceBehavior(Name = "Leads", Namespace = "https://www.microsoft.com/DynamicsGP/Samples/2010")] public class Leads : ILeads {
}
}
Add a method to retrieve a single document.
This method retrieves a single document. Add method parameters that require a document key and a Dynamics GP service context object.
Use the GetByKey method of the GreatPlainsBusinessService to retrieve the specified document. The GetByKey method requires you to supply the following parameters.
Parameter
Description
Key
A Dynamics GP Service key object that specifies a document.
Context
A Dynamics GP Service context object.
Type
Specifies the type of the document to retrieve.
The following code example shows the GetLeadByKey method for the sample Leads service. Notice the use of the typeof() method to specify the document type. Also, notice how the ExceptionManager converts error information to a SOAP exception.
public Lead GetLeadByKey(LeadKey leadKey, Context context)
{ try { GreatPlainsBusinessService service = GreatPlainsBusinessService.GetInstance(); return (Lead)service.GetByKey(leadKey, context, typeof(Lead)); } catch (BusinessException err) { throw ExceptionManager.ConvertBusinessToSoapException(err); } }
Add a method to retrieve a list of summary documents.
This method retrieves a list of summary documents. Add method parameters that require a document criteria and a Dynamics GP Service context object.
Use the GetList method of the GreatPlainsBusinessService to retrieve the specified documents. The GetList method requires you to supply type information for the list. In addition, the GetList method requires you to supply the following parameters.
Parameter
Description
Criteria
A document criteria object that specifies the characteristics of the documents to include in the list.
Context
A Dynamics GP Service context object.
The following code example shows the GetLeadList method for the sample Leads service. Notice how the GetList method of the GreatPlainsBusinessService requires you to specify the document type and the summary type.
public List<LeadSummary> GetLeadList(LeadCriteria criteria, Context context)
{ try { GreatPlainsBusinessService service = GreatPlainsBusinessService.GetInstance(); return service.GetList<Lead, LeadSummary>(criteria, context); } catch (BusinessException err) { throw ExceptionManager.ConvertBusinessToSoapException(err); } }
Add a method to create a document.
This method creates a document. Add method parameters that require a document, a Dynamics GP Service context object, and a document create policy.
Use the Create method of the GreatPlainsBusinessService to insert the document in the Dynamics GP database. The Create method requires you to supply the following parameters.
Parameter
Description
BusinessObject
A Dynamics GP Service business document.
Context
A Dynamics GP Service context object.
Policy
A Dynamics GP Service policy object.
The following code example shows the CreateLead method for the sample Leads service. Notice how the document, context, and policy method parameters are used with the Create method of the GreatPlainsBusinessService.
public void CreateLead(Lead lead, Context context, Policy policy)
{ try { GreatPlainsBusinessService service = GreatPlainsBusinessService.GetInstance(); service.Create(lead, context, policy); } catch (BusinessException err) { throw ExceptionManager.ConvertBusinessToSoapException(err); } }
Add a method to update a document.
This method updates a document. Add method parameters that require a document, a Dynamics GP Service context object, and a document update policy.
Use the Update method of the GreatPlainsBusinessService to update the document in the Dynamics GP database. The Update method requires you to supply the following parameters.
Parameter
Description
BusinessObject
A Dynamics GP Service business document.
Context
A Dynamics GP Service context object.
Policy
A Dynamics GP Service policy object.
The following code example shows the UpdateLead method for the sample Leads service. Notice how the document, context, and policy parameters are used with the Update method of the GreatPlainsBusinessService.
public void UpdateLead(Lead lead, Context context, Policy policy)
{ try { GreatPlainsBusinessService service = GreatPlainsBusinessService.GetInstance(); service.Update(lead, context, policy); } catch (BusinessException err) { throw ExceptionManager.ConvertBusinessToSoapException(err); } }
Add a method to delete a document.
This method deletes a document. Add method parameters that require a document key, a Dynamics GP Service context object, and a document delete policy.
Use the Delete method of the GreatPlainsBusinessService to remove the document from the Dynamics GP database. The Delete method requires you to supply the following parameters.
Parameter
Description
Key
A Dynamics GP Service key object that specifies a document.
Context
A Dynamics GP Service context object.
Policy
A Dynamics GP Service policy object.
Type
Specifies the type of the document to delete.
The following code example shows the DeleteLead method for the sample Leads service. Notice how the document key, context, and policy parameters of the method are used with the Delete method of the GreatPlainsBusinessService. Also, notice the use of the the typeof() method to specify the document type for the Delete method of the GreatPlainsBusinessService.
public void DeleteLead(LeadKey leadKey, Context context, Policy policy)
{ try { GreatPlainsBusinessService service = GreatPlainsBusinessService.GetInstance(); service.Delete(leadKey, context, policy, typeof(Lead)); } catch (BusinessException err) { throw ExceptionManager.ConvertBusinessToSoapException(err); } }