2017-06-04 72 views
2

我有一个解决方案,我有2个项目:动态改变WCF服务的URL在ASP.NET MVC

  1. ASP.NET MVC应用程序使用WCF服务。
  2. 5 WCF服务。

我在项目1中添加了一个Web服务引用。现在,我需要根据用户使用不同的服务,例如,

User Type 1:只允许消费服务1.
User Type 2:只允许消费服务2 等

我有服务URL localhost:6227/Service1.svclocalhost:6227/Service2.svc

我已经储存了所有服务的URL在分贝,我需要改变URL为每个用户类型来消耗他允许的服务,而不只是从增加更多的终点,只有改变URL基于用户类型的后端。我需要相关的链接或代码来解决这个问题。

编辑

Web配置 我在MVC应用程序添加的只是此终结,我不希望使用的网络配置在此处更改地址,但我想在更改地址在应用程序运行时为每个用户类型编码。

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

这可能帮助.. https://stackoverflow.com/questions/5151077/wcf-change-endpoint-address-at-runtime – Abi

+0

它没有帮助。 –

+0

需要考虑的事项:[WCF路由服务](https://msdn.microsoft.com/en-us/library/ee517423(v = vs.110).aspx) –

回答

2

如果我完全意识到你的问题,你需要动态的soap服务调用。也许是这样的:

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

不知道如果我理解正确,但你可以使用下面的代码片段,如果它适合。

//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 
} 
+0

以及如何使用此** reqdURL **仅从该服务提供用户数据? –

+0

你想解决什么问题?动态更改URL或更改服务(而不是URL)发出的数据? – touchofevil

+0

我需要**更改URL **以允许他只使用他的服务。 –