How to pass global parms to web service???

G

Guest

I have a web service with several methods in it. When the user creates an
instance of this WS, I want to pass parameters to the instance:

localhost.WSdemographics myClass= new localhost.WSdemographics(string
Username, string Password);

Once the user creates this instance, then they can use the actual methods in
the service:

DataSet my DS = myClass.GetNames(int RecordGroup);

But when I go to create the instance of 'myClass' I do not get the 2
parameters in the intellisense. It wont even build, b/c it says the class
does not take parms, yet its defiend as

public class WSdemographics : System.Web.Services.WebService
{
public WSdemographics(stringUserName,string Password)
{
...
}
....//WebMethods
....
}
I dont want to have to pass the userName and Password to every moethod in
the service, just at the point its reference. Why cant this be done?
 
A

Ashot Geodakov

Hi JP,

Why not to define the web service like this:

public class WebServiceTest : System.Web.Services.WebService
{
public WebServiceTest()
{
}

[WebMethod]
public void SetCredentials( string u, string p )
{
Context.Cache["UserName"] = u;
Context.Cache["Password"] = p;
}

[WebMethod]
public string GetCredentials()
{
return Context.Cache["UserName"] + ", " + Context.Cache["Password"];
}
}

Then you don't have to pass these username and password to every method.
You can call it in your client app like this:

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
localhost.WebServiceTest test = new
WindowsApplication1.localhost.WebServiceTest();
test.SetCredentials( "username", "password" );
string str = test.GetCredentials();
Console.WriteLine( str );
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top