Creating a Basic Web Services Client in C Sharp Using .NET
To create a simple Entitlement using the EntitlementOrderService wsdl, you will need to create a C# Console Application project with a Web Reference that contains an override function which specifies that requests are to use "Basic" HTTPS authentication, adding a new “Authorization” header with credentials of the form "Basic " plus the base-64-encoded "Name:Password" string.
Creating a Simple Entitlement for .NET
|
1.
|
In Visual Studio 2008 or later, create a new C# Console Application project. |
|
2.
|
In the Solution Explorer, right-click the project icon and select Add Service Reference. |
|
3.
|
Click Advanced at the bottom of the Add Service screen and then Add Web Reference at the bottom of the Service Reference Settings screen. |
|
4.
|
Enter https://<siteID>.flexnetoperations.com/flexnet/services/EntitlementOrderService?wsdl and click Go. Alternatively, you can enter https://<siteID>.flexnetoperations.com/flexnet/services to see a list of all available FlexNet Operations WSDLs. |
|
5.
|
Change the Web reference name to EntOrdSvc and click Add Reference. The Solution Explorer view should have a new EntOrdSvc icon in the Web References folder. |
|
6.
|
Double-click the EntOrdSvc icon, which opens the Object Browser. |
|
7.
|
In the Object Browser, expand WebServiceTest.EntOrdSvc, and double-click one of the items in the tree. This should open the proxy class source file Reference.cs. |
|
8.
|
Near the top of Reference.cs, find the line that begins: |
public EntitlementOrderService()
and insert the following function directly above:
Copy
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request;
request = (HttpWebRequest)base.GetWebRequest(uri);
if (PreAuthenticate)
{
NetworkCredential networkCredentials =
Credentials.GetCredential(uri, "Basic");
if (networkCredentials != null)
{
byte[] credentialBuffer =
new
System.Text.UTF8Encoding( ).GetBytes(
networkCredentials.UserName + ":" +
networkCredentials.Password);
request.Headers["Authorization"] =
"Basic " +
Convert.ToBase64String(credentialBuffer);
}
else
{
throw new ApplicationException(
"No network credentials");
}
}
return request;
}
|
9.
|
At the top of the file in the namespace function for the project, add the following: |
using System.Net;
|
10.
|
In the Program.cs file, add a Web Service request to create an entitlement. For example: |
Copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using WebServiceTest.EntOrdSvc;
namespace WebServiceTest
{
class Program
{
static void Main(string[] args)
{
EntitlementOrderService eos = new EntitlementOrderService();
// begin copied-and-pasted authorization code
NetworkCredential netCredential =
new NetworkCredential("username@company.com", "MyPassword");
Uri uri = new Uri(eos.Url);
ICredentials credentials = netCredential.GetCredential(uri, "Basic");
eos.Credentials = credentials;
eos.PreAuthenticate = true;
Console.WriteLine("[Successfully authenticated...]");
// end copied-and-pasted authorization code
// one and only simple entitlement
createSimpleEntitlementDataType[] seArray = new
createSimpleEntitlementDataType[1];
createSimpleEntitlementDataType se1 = new createSimpleEntitlementDataType();
seArray[0] = se1;
idType idtype = new idType();
// make sure to change the Order ID for each test...
idtype.id = "ExampleOrderID";
se1.entitlementId = idtype;
// make sure this is a real customer account
se1.soldTo = "Atlas";
// one and only line item
createEntitlementLineItemDataType[] lineItems = new
createEntitlementLineItemDataType[1];
se1.lineItems = lineItems;
lineItems[0] = new createEntitlementLineItemDataType();
idtype = new idType();
idtype.id = "ActID-Atlas-123456";
lineItems[0].activationId = idtype;
lineItems[0].numberOfCopies = "5";
lineItems[0].startDate = DateTime.Today;
licenseModelIdentifierType lm = new licenseModelIdentifierType();
licenseModelPKType modelPk = new licenseModelPKType();
// Add a License Model for the desired Product
modelPk.name = "Embedded Counted";
lm.primaryKeys = modelPk;
lineItems[0].licenseModel = lm;
lineItems[0].versionDate = DateTime.Today.Date;
productIdentifierType ordId = new productIdentifierType();
productPKType productPk = new productPKType();
// Be sure to pick a real product and version
productPk.name = "LH Full Access";
productPk.version = "1.0";
ordId.primaryKeys = productPk;
lineItems[0].product = ordId;
// this function is in the auto-generated Reference.cs code
createSimpleEntitlementResponseType csert = eos.createSimpleEntitlement(seArray);
if (csert.statusInfo.status != StatusType.SUCCESS)
{
Console.WriteLine("Drat! Failed: " +
csert.statusInfo.reason);
}
else
{
Console.WriteLine("Success!");
}
}
}
}
|
11.
|
Build and run the application. |