WCF Configuration Enhancement
Background:
In app.config or web.config one may define a config entry in:
<appSettings>...</appSettings>
like so:
<add key="MyKey" value="%SomeEnvironmentVariable%"/>
Thereafter in order to retrieve the value associated with MyKey one may employ the following two lines of code:
string raw = ConfigurationManager.AppSettings[“MyKey”];
string cooked = (raw == null) ? null : Environment.ExpandEnvironmentVariables(raw);
Question:
Is there a way to do the same with the WCF service configuration, for example:
<system.serviceModel>
. . .
<services>
<service name="..." ...>
. . .
<endpoint
address="%MyEndPointAddress%" ... />
. . .
</service>
</services>
</system.serviceModel>
Any knowledge will be highly appreciated.
--Avi
To change endpoint address you will need to know EndPointName and ContractName. This values are found in your config file inside WCF configuration. Then you can use following code:
void SetNewEndpointAddress(string endpointName, string contractName, string newValue)
{
bool settingsFound = false;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ClientSection section = config.GetSection("system.serviceModel/client") as ClientSection;
foreach (ChannelEndpointElement ep in section.Endpoints)
{
if (ep.Name == endpointName && ep.Contract == contractName)
{
settingsFound = true;
ep.Address = new Uri(newValue);
config.Save(ConfigurationSaveMode.Full);
}
}
if (!settingsFound)
{
throw new Exception(string.Format("Settings for Endpoint '{0}' and Contract '{1}' was not found!", endpointName, contractName));
}
}
Happy coding!
Related
in my application I am referencing a WSDL service by "Add Service Reference"
then a service reference is created. with this entry in web.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IExternalOrder" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://xx.xxx.com/ExternalOrder.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IExternalOrder"
contract="WebReference.IExternalOrder" name="BasicHttpBinding_IExternalOrder" />
</client>
</system.serviceModel>
at this point I am trying to call this WSDL functions, in order to get the data that I need:
var GetOrderDetails_answer = ServiceHelper.GetOrderDetails(creds, this.OrderId);
inside ServiceHelper class:
public static GetAdminOrderDetailResponse GetOrderDetails(Request creds, string orderId)
{
////////////////////////////////////////////////////
ExternalOrderClient EOC = new ExternalOrderClient();
////////////////////////////////////////////////////
var GAOD = new GetAdminOrderDetailRequest
{
OrderID = orderId,
Password = creds.Password,
SiteID = creds.SiteID,
UserName = creds.UserName
};
try
{
return EOC.GetAdminOrderDetail(GAOD);
}
catch (Exception e)
{
/// TODO: log the error
return null;
}
}
The creation of the object ExternalOrderClient between "/////////////" lines
is throwing an exception:
Could not find default endpoint element that references contract
'WebReference.IExternalOrder' in the ServiceModel client configuration
section.
I tried every solution I could find (most of them are about calling the WSDL from other project, but i have only 1 project)
What is the fix for that?
I'm using WCF routing service and I'm trying to Implement failover, I need to add filter table backuplist programmatically, this is a sample configuration:
<system.serviceModel>
<client>
<endpoint address="http://localhost:8081/Service1" binding="basicHttpBinding"
contract="*" name="ServiceOperation1" />
<endpoint address="http://localhost:8081/Service2" binding="basicHttpBinding"
contract="*" name="ServiceOperation2" />
<endpoint address="http://localhost:8081/Service3" binding="basicHttpBinding"
contract="*" name="ServiceOperation3" />
</client>
<routing>
<filters>
<filter name="MatchAllFilter" filterType="MatchAll" />
</filters>
<filterTables>
<filterTable name="RoutingTable">
<add filterName="MatchAllFilter" endpointName="ServiceOperation1" backupList="BackUps" />
</filterTable>
</filterTables>
<backupLists>
<backupList name="BackUps">
<add endpointName="ServiceOperation2"/>
<add endpointName="ServiceOperation3" />
</backupList>
</backupLists>
</routing>
<behaviors>
<serviceBehaviors>
<behavior name="">
<routing filterTableName="RoutingTable" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="System.ServiceModel.Routing.RoutingService">
<endpoint address="binary" binding="basicHttpBinding"
contract="System.ServiceModel.Routing.IRequestReplyRouter" name="VirtualEndpoint" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/RoutingService/Router" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
I was able to add FilterTable which I found example in this question
here is my code snippet:
var routingHost = new ServiceHost(typeof(RoutingService));
var routingEp = routingHost.AddServiceEndpoint(typeof(System.ServiceModel.Routing.IRequestReplyRouter), mybinding, url);
var filterTable = new MessageFilterTable<IEnumerable<ServiceEndpoint>>();
filterTable.Add(new MatchAllMessageFilter(), new List<ServiceEndpoint>()
{
serviceoperation1Endpoint
});
routingHost.Description.Behaviors.Add(
new RoutingBehavior(new RoutingConfiguration(filterTable, false)));
routingHost.open();
so in my scenario ServiceOperation2 and ServiceOperation3 are the backup endpoints, I made lots of research I coudn't find a way to add backuplist Programmatically
any Idea how can I add backuplist to filterTable?
I ended up with this solution to dynamically generate config file
In my scenario, I load my endpoints from database and generated routing service configuration out of it,
public class MyServiceEndPoint
{
public string TypeName { get; set; }
public string Url { get; set; }
public string Name { get; set; }
}
//// generates routing service configuration section, including client enoints/filterTable/backups and routing service behavior
private void CreateRoutingConfiguration(List<MyServiceEndPoint> serviceEndpoints)
{
///// group endopints by Name, each service could have multiple endpoints ( 1 main and n backup endpoints)
var groupedEndpoitns = (from endp in serviceEndpoints
group endp by endp.Name into endpGroup
select new { ServiceName = endpGroup.Key, EndPoint = endpGroup }).ToList();
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var serviceModelSectionGroup = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
var routingSection = (RoutingSection)serviceModelSectionGroup.Sections["routing"];
var clientsection = (ClientSection)serviceModelSectionGroup.Sections["client"];
var bindingSection = (BindingsSection)serviceModelSectionGroup.Sections["bindings"];
var behaviorSection = (BehaviorsSection)serviceModelSectionGroup.Sections["behaviors"];
bindingSection.NetTcpBinding.Bindings.Clear();
clientsection.Endpoints.Clear();
var filterTable = new FilterTableEntryCollection() { Name = "RoutingTable" };
routingSection.Filters.Clear();
routingSection.FilterTables.Clear();
routingSection.BackupLists.Clear();
var nettcpBinding = new NetTcpBindingElement()
{
Name = "myTcpBinding",
TransferMode = TransferMode.Buffered,
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
SendTimeout = new TimeSpan(0, 10, 0),
ReceiveTimeout = new TimeSpan(0, 10, 0),
};
nettcpBinding.Security.Mode = SecurityMode.None;
bindingSection.NetTcpBinding.Bindings.Add(nettcpBinding);
foreach (var endpointGroup in groupedEndpoitns)
{
var backupListItem = new BackupEndpointCollection();
backupListItem.Name = endpointGroup.ServiceName + "Backup";
var filter = new FilterElement();
filter.Name = endpointGroup.ServiceName + "Filter";
filter.FilterType = FilterType.Custom;
filter.CustomType = "MyServiceContractMessageFilterType,asemblyName";
filter.FilterData = endpointGroup.EndPoint.FirstOrDefault().ClientTypeName;
routingSection.Filters.Add(filter);
int endpointCount = 0;
List<ChannelEndpointElement> channelEndpoints = new List<ChannelEndpointElement>();
foreach (var endpoint in endpointGroup.EndPoint)
{
endpointCount++;
var channelEndpoint = new ChannelEndpointElement();
channelEndpoint.Address = new Uri(endpoint.Url);
channelEndpoint.Binding = "netTcpBinding";
channelEndpoint.BindingConfiguration = "myTcpBinding";
channelEndpoint.Contract = "*";
channelEndpoint.Name = $"{endpoint.Name}EndPoint{endpointCount}";
clientsection.Endpoints.Add(channelEndpoint);
channelEndpoints.Add(channelEndpoint);
}
var firstChannelEndpoint = channelEndpoints.FirstOrDefault(); /// this endpoint will be selected as main endpoint
var filterTableItem = new FilterTableEntryElement();
filterTableItem.FilterName = filter.Name;
filterTableItem.EndpointName = firstChannelEndpoint.Name;
filterTableItem.BackupList = backupListItem.Name;
filterTable.Add(filterTableItem);
foreach (var backupEndpoints in channelEndpoints)
{
backupListItem.Add(new BackupEndpointElement() { EndpointName = backupEndpoints.Name });
routingSection.BackupLists.Add(backupListItem);
}
}
routingSection.FilterTables.Add(filterTable);
behaviorSection.ServiceBehaviors.Clear();
var behavior = new ServiceBehaviorElement();
behavior.Add(new RoutingExtensionElement() { FilterTableName = filterTable.Name });
behaviorSection.ServiceBehaviors.Add(behavior);
config.Save(ConfigurationSaveMode.Modified, false);
ConfigurationManager.RefreshSection("system.serviceModel/routing");
ConfigurationManager.RefreshSection("system.serviceModel/client");
ConfigurationManager.RefreshSection("system.serviceModel/behaviors");
}
so first I generated configuration file
and than create an intance of routing service like:
CreateRoutingConfiguration(serviceEndpoints);
routingHost = new ServiceHost(typeof(RoutingService));
routingHost.AddServiceEndpoint(typeof(System.ServiceModel.Routing.IRequestReplyRouter), mybinding, $"net.tcp://localhost:6000/Router");
routingHost.Open();
hope it will be helpful for someone
I've never done this, but a quick look through the documentation on MSDN for Message Filters shows that alternative backup endpoints are configured via the FilterTableElementEntry class (BackupList property).
A filter table is a named collection of FilterTableEntryElement
objects that define the association between a filter, a primary
destination endpoint, and a list of alternative backup endpoints. The
filter table entries also allow you to specify an optional priority
for each filter condition.
Check for Filter Table and BackupList on Google, and you'll come across examples of usage of this. This example looks particularly promising with plenty of comments describing the steps.
I'm busy writing a file server/client tool that basically uses a hosted Service to send and receive data to and from the server. Since this solution will be used by many different people, its not really advisable to have them go and edit the App.Config file for their setup. What I would like to do is change this at runtime so that the user(s) have full control over the settings to use. So, this is my App.Config file:
<system.serviceModel>
<services>
<service name="FI.ProBooks.FileSystem.FileRepositoryService">
<endpoint name="" binding="netTcpBinding"
address="net.tcp://localhost:5000"
contract="FI.ProBooks.FileSystem.IFileRepositoryService"
bindingConfiguration="customTcpBinding" />
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="customTcpBinding" transferMode="Streamed" maxReceivedMessageSize="20480000" />
</netTcpBinding>
</bindings>
</system.serviceModel>
What I would like to do is to change only the address (in this example, net.tcp://localhost:5000) when the application is executed. So I must be able to read the current value and display that to the user, and then take their input and save it back into that field.
The test below may help you. Essentially the steps are
Instantiate an instance of the host that reads the configuration from the .config file;
Create a new instance of EndpointAddress using the same configuration as the old one, but changing the uri and assign it to the Address property of your ServiceEndpoint.
[TestMethod]
public void ChangeEndpointAddressAtRuntime()
{
var host = new ServiceHost(typeof(FileRepositoryService));
var serviceEndpoint = host.Description.Endpoints.First(e => e.Contract.ContractType == typeof (IFileRepositoryService));
var oldAddress = serviceEndpoint.Address;
Console.WriteLine("Curent Address: {0}", oldAddress.Uri);
var newAddress = "net.tcp://localhost:5001";
Console.WriteLine("New Address: {0}", newAddress);
serviceEndpoint.Address = new EndpointAddress(new Uri(newAddress), oldAddress.Identity, oldAddress.Headers);
Task.Factory.StartNew(() => host.Open());
var channelFactory = new ChannelFactory<IFileRepositoryService>(new NetTcpBinding("customTcpBinding"), new EndpointAddress(newAddress));
var channel = channelFactory.CreateChannel();
channel.Method();
(channel as ICommunicationObject).Close();
channelFactory = new ChannelFactory<IFileRepositoryService>(new NetTcpBinding("customTcpBinding"), oldAddress);
channel = channelFactory.CreateChannel();
bool failedWithOldAddress = false;
try
{
channel.Method();
}
catch (Exception e)
{
failedWithOldAddress = true;
}
(channel as ICommunicationObject).Close();
Assert.IsTrue(failedWithOldAddress);
}
you can create the service instance providing a configuration name and endpoint. So you can use;
EndpointAddress endpoint = new EndpointAddress(serviceUri);
var client= new MyServiceClient(endpointConfigurationName,endpoint )
look at msdn article.
Here I am trying to read my service endpoint address by name from web.config
ClientSection clientSection = (ClientSection)ConfigurationManager.GetSection("system.serviceModel/client");
var el = clientSection.Endpoints("SecService"); // I don't want to use index here as more endpoints may get added and its order may change
string addr = el.Address.ToString();
Is there a way I can read end point address based on name?
Here is my web.config file
<system.serviceModel>
<client>
<endpoint address="https://....................../FirstService.svc" binding="wsHttpBinding" bindingConfiguration="1ServiceBinding" contract="abc.firstContractName" behaviorConfiguration="FirstServiceBehavior" name="FirstService" />
<endpoint address="https://....................../SecService.svc" binding="wsHttpBinding" bindingConfiguration="2ServiceBinding" contract="abc.secContractName" behaviorConfiguration="SecServiceBehavior" name="SecService" />
<endpoint address="https://....................../ThirdService.svc" binding="wsHttpBinding" bindingConfiguration="3ServiceBinding" contract="abc.3rdContractName" behaviorConfiguration="ThirdServiceBehavior" name="ThirdService" />
</client>
</system.serviceModel>
This will work clientSection.Endpoints[0];, but I am looking for a way to retrieve by name.
I.e. something like clientSection.Endpoints["SecService"], but it's not working.
This is how I did it using Linq and C# 6.
First get the client section:
var client = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
Then get the endpoint that's equal to endpointName:
var qasEndpoint = client.Endpoints.Cast<ChannelEndpointElement>()
.SingleOrDefault(endpoint => endpoint.Name == endpointName);
Then get the url from the endpoint:
var endpointUrl = qasEndpoint?.Address.AbsoluteUri;
You can also get the endpoint name from the endpoint interface by using:
var endpointName = typeof (EndpointInterface).ToString();
I guess you have to actually iterate through the endpoints:
string address;
for (int i = 0; i < clientSection.Endpoints.Count; i++)
{
if (clientSection.Endpoints[i].Name == "SecService")
address = clientSection.Endpoints[i].Address.ToString();
}
Well, each client-side endpoint has a name - just instantiate your client proxy using that name:
ThirdServiceClient client = new ThirdServiceClient("ThirdService");
Doing this will read the right information from the config file automatically.
I'm trying to add ad-hoc discovery to a simple WCF service-client setup (currently implemented by self hosting in a console app). Debugging using VS2010 on windows 7, and doing whatever I can find in online tutorial, but still - the discovery client simply finds nothing. Needless to say if I open a client to the correct service endpoint I can access the service from the client.
service code:
using (var selfHost = new ServiceHost(typeof(Renderer)))
{
try
{
selfHost.Open();
...
selfHost.Close();
service app.config:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="TestApp.Renderer">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9000" />
</baseAddresses>
</host>
<endpoint address="ws" binding="wsHttpBinding" contract="TestApp.IRenderer"/>
<endpoint kind="udpDiscoveryEndpoint"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDiscovery/>
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
client discovery code:
DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
var criteria = new FindCriteria(typeof(IRenderer)) { Duration = TimeSpan.FromSeconds(5) };
var endpoints = discoveryClient.Find(criteria).Endpoints;
The 'endpoints' collection always comes out empty. I've tried running the service and client from the debugger, from a command line, from an admin command line - everything, but to no avail (all on the local machine, of course, not to mantion I'll need it running on my entire subnet eventually)
Any help would be appreciated :-)
Here is a super simple discovery example. It does not use a config file, it is all c# code, but you can probably port the concepts to a config file.
share this interface between host and client program (copy to each program for now)
[ServiceContract]
public interface IWcfPingTest
{
[OperationContract]
string Ping();
}
put this code in the host program
public class WcfPingTest : IWcfPingTest
{
public const string magicString = "djeut73bch58sb4"; // this is random, just to see if you get the right result
public string Ping() {return magicString;}
}
public void WcfTestHost_Open()
{
string hostname = System.Environment.MachineName;
var baseAddress = new UriBuilder("http", hostname, 7400, "WcfPing");
var h = new ServiceHost(typeof(WcfPingTest), baseAddress.Uri);
// enable processing of discovery messages. use UdpDiscoveryEndpoint to enable listening. use EndpointDiscoveryBehavior for fine control.
h.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
h.AddServiceEndpoint(new UdpDiscoveryEndpoint());
// enable wsdl, so you can use the service from WcfStorm, or other tools.
var smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
h.Description.Behaviors.Add(smb);
// create endpoint
var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
h.AddServiceEndpoint(typeof(IWcfPingTest) , binding, "");
h.Open();
Console.WriteLine("host open");
}
put this code in the client program
private IWcfPingTest channel;
public Uri WcfTestClient_DiscoverChannel()
{
var dc = new DiscoveryClient(new UdpDiscoveryEndpoint());
FindCriteria fc = new FindCriteria(typeof(IWcfPingTest));
fc.Duration = TimeSpan.FromSeconds(5);
FindResponse fr = dc.Find(fc);
foreach(EndpointDiscoveryMetadata edm in fr.Endpoints)
{
Console.WriteLine("uri found = " + edm.Address.Uri.ToString());
}
// here is the really nasty part
// i am just returning the first channel, but it may not work.
// you have to do some logic to decide which uri to use from the discovered uris
// for example, you may discover "127.0.0.1", but that one is obviously useless.
// also, catch exceptions when no endpoints are found and try again.
return fr.Endpoints[0].Address.Uri;
}
public void WcfTestClient_SetupChannel()
{
var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
var factory = new ChannelFactory<IWcfPingTest>(binding);
var uri = WcfTestClient_DiscoverChannel();
Console.WriteLine("creating channel to " + uri.ToString());
EndpointAddress ea = new EndpointAddress(uri);
channel = factory.CreateChannel(ea);
Console.WriteLine("channel created");
//Console.WriteLine("pinging host");
//string result = channel.Ping();
//Console.WriteLine("ping result = " + result);
}
public void WcfTestClient_Ping()
{
Console.WriteLine("pinging host");
string result = channel.Ping();
Console.WriteLine("ping result = " + result);
}
on the host, simply call the WcfTestHost_Open() function, then sleep forever or something.
on the client, run these functions. It takes a little while for a host to open, so there are several delays here.
System.Threading.Thread.Sleep(8000);
this.server.WcfTestClient_SetupChannel();
System.Threading.Thread.Sleep(2000);
this.server.WcfTestClient_Ping();
host output should look like
host open
client output should look like
uri found = http://wilkesvmdev:7400/WcfPing
creating channel to http://wilkesvmdev:7400/WcfPing
channel created
pinging host
ping result = djeut73bch58sb4
this is seriously the minimum I could come up with for a discovery example. This stuff gets pretty complex fast.
Damn! it was the firewall... for some reason all UDP communication was blocked - disabling the firewall solved the problem. Now I only need to figure out the correct firewall configuration...