donderdag 7 juli 2011

Master Data Services – Working with attributes using WCF API

Introduction

In the article "Master Data Services: Find your way through the MDS database schema" we have read and updated attribute values by accessing the MDS database directly via T-SQL. Using the direct approach your code could break when a newer version of the MDS database schema is released, and that’s why it’s better to use the Master Data Services WCF API. In this article I am using the Master Data Services WCF API to update attribute values with auto-generated values, which is an enhancement to the code that has been used in the article "Master Data Services: Implementing a Custom Workflow " where T-SQL is used to directly update attribute values.

Context

A business rule is triggered when a new item has been created; a XML message will be queued in the Service Broker and handled by the SQL Server MDS Workflow Integration service that calls the custom .NET assembly, which uses calls to the Master Data Services WCF API to update attribute values.


Configuration

When you want to make use of the Master Data Services WCF API you have to enable the webservice for your MDS web application in the Master Data Services Configuration Manager.



After you created a website, behind the scenes several settings will be added to the web.config (can be found in the folder “C:\Program Files\Microsoft SQL Server\Master Data Services\WebApplication”). In the “masterDataServices” section the Master Data Manager configuration is configured, in the “connectionStrings” section the connection string to the MDS database, and in the “services” section the binding with the WCF web service. The configuration settings within the different sections are related to each other by labels (in this case “MDS1”).

<masterDataServices>
<instance virtualPath="MDS" siteName="Default Web Site" connectionName="MDS1" serviceName="MDS1" />
masterDataServices>

<
connectionStrings>
<add name="MDS1" connectionString="Data Source=OSG3002K8;Initial Catalog=MDS;Integrated
     Security=True;Connect Timeout=3600" />
connectionStrings>

<service behaviorConfiguration="mdsWsHttpBehavior" name="MDS1">
<endpoint binding="wsHttpBinding" bindingConfiguration="mdsWsHttpBinding"
                  bindingNamespace="http://schemas.microsoft.com/sqlserver/masterdataservices/2009/09"
                  contract="Microsoft.MasterDataServices.Services.ServiceContracts.IService" />
service>

Update attribute

To be able to use the Master Data Services WCF API you have to add a service reference to your customer .NET assembly project. If working locally, this service reference must reference the service http://localhost/MDS/service/service.svc. The following snippet of code shows how to update an attribute value by using the Master Data Services WCF API (the input variables are colored purple).

WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = System.ServiceModel.SecurityMode.Message;
binding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
EndpointAddress address = new
        EndpointAddress(webserviceUrl);


ChannelFactory<MDSWS.IService> channelFactory = new ChannelFactory<MDSWS.IService>(binding, address);
MDSWS.IService client = channelFactory.CreateChannel();

//Create request and response objects
MDSWS.EntityMembersUpdateRequest request = new MDSWS.EntityMembersUpdateRequest();
MDSWS.EntityMembersUpdateResponse response = new MDSWS.EntityMembersUpdateResponse();

//Create member context
MDSWS.EntityMembers memberContext = new MDSWS.EntityMembers();
memberContext.ModelId = new MDSWS.Identifier() { Name = modelName };
memberContext.EntityId = new MDSWS.Identifier() { Name = entityName };
memberContext.VersionId = new MDSWS.Identifier() { Name = versionName };
request.Members = memberContext;

//Create member collection
Collection<MDSWS.Member> memberCollection = new Collection<MDSWS.Member>();
memberContext.Members = memberCollection;

//Create member
MDSWS.Member member = new MDSWS.Member();
member.MemberId = new MDSWS.MemberIdentifier() { Code = memberCode };
memberCollection.Add(member);

//Create attributes
member.Attributes = new Collection<MDSWS.Attribute>();
MDSWS.Attribute attribute = new MDSWS.Attribute();
attribute.Identifier = new MDSWS.Identifier() { Name = attributeName };
attribute.Value = attributeValue;
member.Attributes.Add(attribute);

//update attribute
response = client.EntityMembersUpdate(request);
foreach (MDSWS.Error error in response.OperationResult.Errors)
{
//error handling
}

((IClientChannel)client).Close();
channelFactory.Close();

Security

When the Master Data Services WCF API is used from a custom .NET assembly, it is running under the same credentials as the custom .NET assembly runs that inherit its credentials from the SQL Server MDS Workflow Integration service. When you use an account for the SQL Server MDS Workflow Integration service that has read-only permission for an attribute that you want to update like described in the previous section, you will get an error (Code = “300014”, Description = The attribute is read-only. You do not have permission to update the attribute.). You can solve this problem by using a service account for the SQL Server MDS Workflow Integration service that has update permissions for the models, entities, and attributes in MDS (has to be set in the Master Data Manager) or impersonate the custom .NET assembly to an account that does have update permissions.

Conclusion

Using the Master Data Services WCF API is the preferred way to update attribute values. You need a small learning curve to find your way through the API, but when you’ve done this, it works like a charm.

References

Geen opmerkingen:

Een reactie plaatsen