interface for web service without a web reference

Purpose…

Sometimes one has a project where one must dynamically determine which web service to call.

In a typical VS.NET web site project, a web reference is needed to call such a service– but that web reference, when added in the standard fashion, points to a specific URL (web service) and it the URL for that service is not assigned dynamically.

Well, if one needs to determine the URL for the service to call at run-time, and if a typcial web reference cannot do that, then one must call the web service without a web reference.

It is possible to dynamically get a reference to a service, but how do you know the API for that service?

Simple, make sure the service implements a generic interface.

Voila.

One good answer to this is below.

(Make sure you get the latest version available.)

Version 1…

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Test.Framework.Interfaces.BusinessLayer.BusinessEntities
{
	/// <summary>
	/// This is a shared interface for web services that must conform to a standard API and be called without a web reference.
	/// </summary>
	/// <remarks>
	/// Note that the targetIdList parameters are XML, not delimited lists, so data type can be specified if necessary.
	/// Note that this interface is designed for Fire-And-Forget operations, except where a return is absolutely required.
	/// Note that each method has a unique name to simplify attribute creation in the implementing code.
	/// Note that details concerning how and why one might call a web service in this way are found at the following link...
	/// http://www.codeproject.com/KB/cpp/CallWebServicesDynamic.aspx
	/// </remarks>
	public interface IGenericWebServiceStub
	{
		#region InterfaceMethods

		/// <summary>
		/// This saves the given objects.
		/// </summary>
		/// <param name="targetObjectList">This is a list of zero-to-many objects, such as a DataSet serialized as XML.</param>
		void Create(string targetObjectList);

		/// <summary>
		/// This deletes each object that has an ID in the given list.
		/// </summary>
		/// <param name="targetIdList">This is a list of IDs, such as a DataSet serialized as XML.</param>
		void Delete(string targetIdList);

		/// <summary>
		/// This initializes a new object and returns it, without having saved the object.
		/// </summary>
		/// <returns>This is a list containing exactly-one object, such as a DataSet serialized as XML.</returns>
		string Initialize();

		/// <summary>
		/// This retrieves each object that has an ID in the given list.
		/// </summary>
		/// <param name="targetIdList">This is a list of IDs, such as a DataSet serialized as XML.</param>
		/// <returns>This is a list of zero-to-many objects, such as a DataSet serialized as XML.</returns>
		string Retrieve(string targetIdList);

		/// <summary>
		/// This retrieves all objects.
		/// </summary>
		/// <returns>This is a list of zero-to-many objects, such as a DataSet serialized as XML.</returns>
		string RetrieveAll();

		/// <summary>
		/// This retrieves the count of existing objects.
		/// </summary>
		/// <returns>This is the count of objects in the data store.</returns>
		long RetrieveCount();

		/// <summary>
		/// This executes a run-operation.
		/// </summary>
		void Run();

		/// <summary>
		/// This updates the given objects.
		/// </summary>
		/// <param name="targetObjectList">This is a list of zero-to-many objects, such as a DataSet serialized as XML.</param>
		void Update(string targetObjectList);

		#endregion //InterfaceMethods
	}
}

Version 2…

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Test.Framework.Interfaces.BusinessLayer.BusinessEntities
{
	/// <summary>
	/// This is a generic interface for services called without a web reference.
	/// </summary>
	/// <remarks>
	/// Note that this interface is designed for mainly Fire-And-Forget operations.
	/// Note that each method has a single parameter, to ensure uniform call sites.
	/// Note one implementation takes the same a DataSet serialized as XML as a DTO.
	/// Note one implementation uses BusinessLayer-generated, single-column, Guid, PkIds.
	/// Note details concerning how and why one might call a web service as such here...
	/// http://www.codeproject.com/KB/cpp/CallWebServicesDynamic.aspx
	/// </remarks>
	public interface IGenericService
	{
		/// <summary>
		/// This saves the given objects.
		/// </summary>
		/// <param name="targetData">This is a list of zero-to-many objects.</param>
		void Create(string targetData);

		/// <summary>
		/// This deletes each object that has an ID in the given list.
		/// </summary>
		/// <param name="targetData">This is a list of zero-to-many objects, perhaps with just IDs.</param>
		void Delete(string targetData);

		/// <summary>
		/// This initializes a new object, gives it a PkId, but does not save it.
		/// </summary>
		/// <param name="targetData">This is the data to use, typically not necessary.</param>
		/// <returns>This is a list containing exactly-one object.</returns>
		string Initialize(string targetData);

		/// <summary>
		/// This retrieves zero-to-many objects based on the given data.
		/// </summary>
		/// <param name="targetData">This is a list of zero-to-many objects, perhaps with just IDs.</param>
		/// <returns>This is a list of zero-to-many objects.</returns>
		string Retrieve(string targetData);

		/// <summary>
		/// This updates the given objects.
		/// </summary>
		/// <param name="targetData">This is a list of zero-to-many objects.</param>
		void Update(string targetData);
	}
}

HTH.

Thank you.

— Mark Kamoski

Advertisements

Author: mkamoski1

n/a