[This document supports a preliminary release of a software product that may be changed substantially prior to final commercial release. This document is provided for informational purposes only.]
As described in Conditional Operations using SOAP Interface (C#), the following example illustrates the conditional GET operation.
The application performs the following steps:
-
Create a sample "book" entity. The
Scope returned by this operation contains the system assigned version number of the entity.
-
Issue a conditional GET request using the
Scope object returned by Create call in the previous step. For conditional GET, the application sets the scopeReturned.VersionMatch.MatchType property of the Scope object so that entity is retrieved only if server has updated version. If the GET fails, then the application handles the "EntityNotModified" condition.
To create a working sample,
-
Add a service reference. For detail steps, see >Examples of using SOAP Interface with the SQL Server Data ServicesExamples of Using SOAP Interface with the SQL Data Services.
-
Update code and provide your existing authority id, container id. Also provide id for the sample entity that application creates.
-
Provide credentials (user name, password) when application executes.
-
In the code below, the "
using ConditionalGetSOAP.ssdsClient" statement has two parts:
-
The first part (
ConditionalGetSOAP) is the namespace name. This namespace name must match the Default namespace of the application.
-
The second part (
ssdsClient) is the name you provided when you added the service reference.
using ConditionalGetSoap.ssdsClient;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;
using System.Net;
namespace ConditionalGetSoap
{
class Program
{
// Provide your own values for these member variables
private static string userName;
private static string password;
private const string authorityId = "<YourExistingAuthorityId>";
private const string containerId = "<YourExistingContainerId>";
// entity to create for testing update.
private const string sampleEntityId = "<SampleEntityId>";
private static SitkaSoapServiceClient proxy;
static void Main(string[] args)
{
Console.Write("Username: ");
userName = Console.ReadLine();
Console.Write("Password: ");
password = ReadPassword();
using (proxy = new SitkaSoapServiceClient("BasicAuthEndpoint"))
{
proxy.ClientCredentials.UserName.UserName = userName;
proxy.ClientCredentials.UserName.Password = password;
// 1) First create a sample entity (to update later)
// Set scope to the container where you want to create the sample entity
Scope myContainerScope = new Scope();
myContainerScope.AuthorityId = authorityId;
myContainerScope.ContainerId = containerId;
// Construct a sample book entity to use for this sample.
Entity e1 = CreateSampleEntity(sampleEntityId, "UsedBook");
Scope scopeReturned = new Scope();
try
{
// Create the Entity within the Service.
scopeReturned = proxy.Create(myContainerScope, e1);
// Next, attempt to retrieve the entity but only do so if the entity content has
// changed since we created it.
scopeReturned.VersionMatch.MatchType = VersionMatchType.NotMatch;
Entity myBook = proxy.Get(scopeReturned);
}
catch (FaultException<Error> ex)
{
if (ex.Detail.StatusCode == ErrorCodes.EntityNotModified)
{
Console.WriteLine("You have the latest version of this entity");
}
else
{
Console.WriteLine("Operation failed! {0}", ex);
Console.WriteLine(ex.Detail.Message);
}
}
catch (CommunicationException e)
{
// Use this to echo out invalid credentials case.
Console.WriteLine("Unexpected error: {0}!", e.Message);
}
}
}
private static Entity CreateSampleEntity(string id, string kind)
{
// Check inbound parameters.
if (String.IsNullOrEmpty(id))
{
throw new ArgumentOutOfRangeException("id");
}
if (String.IsNullOrEmpty(kind))
{
throw new ArgumentOutOfRangeException("kind");
}
// Construct the entity.
Entity newEntity = new Entity();
// Set the metadata properties.
newEntity.Id = id;
newEntity.Kind = kind;
// Set some example flex properties.
newEntity.Properties = new Dictionary<string, object>();
newEntity.Properties["Title"] = "My Test Book To Update";
newEntity.Properties["ISBN"] = "1-57880-066-3";
newEntity.Properties["Author"] = "Mr. Author";
newEntity.Properties["Publisher"] = "Mr. Publisher";
newEntity.Properties["InPrint"] = true;
newEntity.Properties["NumberOfCopiesSold"] = 5002m;
newEntity.Properties["PublicationDate"] = DateTime.Parse("01/27/2007");
newEntity.Properties["CoverPhoto"] = new byte[] { 0x1, 0x2, 0x3 };
return newEntity;
}
private static string ReadPassword()
{
StringBuilder retval = new StringBuilder();
ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
{
Console.Write("*");
retval.Append(keyInfo.KeyChar);
}
Console.WriteLine();
return retval.ToString();
}
}
}
Concepts
Conditional Operations using SOAP Interface (C#)