Shahzad
Shahzad

Reputation: 1415

Dynamically Change WCF Service URL in ASP.NET MVC

I have one solution in which I have 2 projects with:

  1. ASP.NET MVC Application to consume wcf services.
  2. 5 WCF services.

I have added one web service reference in the project 1. Now, I need to use different services based on the user e.g.

User Type 1: Only allow to consume Service 1.
User Type 2: Only allow to consume Service 2. etc.

I have Service URL's like localhost:6227/Service1.svc, localhost:6227/Service2.svc etc.

I have stored all service URL's in the db and I need to change URL for each user type to consume his allowed service only without adding more end points and only change URL from the backend based on user type. I need relevant link or code to solve this problem.

Edit

In Web Config I have added just this endpoint in the mvc application and I don't want to use web config to change address in here but I want to change address in the code for each user type while application is running.

<client>
      <endpoint address="http://localhost:6227/Service1.svc"
        binding="customBinding" bindingConfiguration="CustomBinding_IService1"
        contract="Service1.IService1" name="CustomBinding_IService1" />
    </client>

Upvotes: 2

Views: 2878

Answers (3)

Adad ML
Adad ML

Reputation: 1

You can modify directly your EndPoint Address doing this:

ClientService1 ws = new ClientService1();
ws.Endpoint.Address = new EndpointAddress("Your new URL svc service");

Upvotes: 0

Mohammad
Mohammad

Reputation: 2764

if i completely realize your question you need dynamic soap service calling. maybe something like this:

private void CallService()
{
    var myBinding = new BasicHttpBinding();
    myBinding.Security.Mode = BasicHttpSecurityMode.None;
    var myEndpointAddress = new EndpointAddress("your url depend on user type");
    var client = new ClientService1(myBinding, myEndpointAddress);
    var outpiut = client.someCall();
    client.close();
}

Upvotes: 3

touchofevil
touchofevil

Reputation: 613

Not sure if I understand you correctly but you could use below snippet, if it suits.

//assuming you get string URL. Change type as per need.
string reqdURL = GetServiceURL(typeof(userObject));

private string GetServiceURL(Type userType)
{
    if (userType == typeof(UserType1))
    {
        // currently hardcoded but you can replace your own fetch logic
        return "localhost:6227/Service1.svc";
    }
    if (userType == typeof(UserType2))
    {
        return "localhost:6227/Service2.svc";
    }
    //and so on
}

Upvotes: 0

Related Questions