简体   繁体   中英

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 eg

: Only allow to consume Service 1. :只允许使用服务 1。
: Only allow to consume Service 2. etc. : 只允许消费Service 2等。

I have Service URL like localhost:6227/ , localhost:6227/ etc.localhost:6227/ localhost:6227/ 等。

I have stored all service URL's in the 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.,我需要更改每个用户类型的 URL 以仅使用他允许的服务而不添加更多端点,并且仅根据用户类型从后端更改 URL。 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>

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();
}

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
}

You can modify directly your EndPoint Address doing this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM