Is there any way to configure WCF Service Reference from configuration file? I would like to configure WCF service reference with such settings as SecurityMode, Address, ReaderQuotas etc. I would also like to be able to choose between WsHttpBinding, BasicHttpBinding, BasicHttpsBinging etc (like normal configuration provided by app.config in .NET Framework).
Is there any way to achive that in .NET Core/.NET Standard?
Thank, Bartek
Certain binding, such as Wshttpbinding,Netnamedbinding is not compatible with DotNet Core framework. Consequently, we could not configure it. However, this doesn’t represent that we can’t configure Basichttpbinding, Nettcpbinding.
At present, the WCF service cannot be created by using DotNet Core without using the third-party library. Moreover, WCF client based on DotNet Core just a compatible workaround.
https://github.com/dotnet/wcf
Like the DotNet Framework project, Microsoft Corporation provides Microsoft WCF Web Service Reference Provider tool to generate a client proxy.
https://learn.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide
After adding connected service, it shall generate a new namespace contains the client proxy class. Most of the client configuration located in the Reference.cs.
Also, we could manually program the code to call the WCF service.
class Program
{
static void Main(string[] args)
{
//using the automatically generated client proxy lcoated in the Reference.cs file to call the service.
//ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
//var result = client.TestAsync();
//Console.WriteLine(result.Result);
//using the Channel Factory to call the service.
Uri uri = new Uri("http://10.157.13.69:21012");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
IService service = factory.CreateChannel();
var result = service.Test();
Console.WriteLine(result);
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string Test();
}
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
Feel free to let me know if there is anything I can help with.
Related
I have a WCF service, which was developed using the .Net framework 4.7.
Now I have to validate & Parse the WCF Service programmatically using .Net Core3.1 Web Application without adding the WCF Service as a Service Reference/Add Connected Service options in Visual Studio Solution Explorer
We can also use the channel factory to call WCF services, this method does not need to add a service reference,here is a demo:
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
var address = new EndpointAddress("http://localhost:801/Service1.svc/Service");
var factory = new ChannelFactory<IService1>(basicHttpBinding, address);
IService1 channel = factory.CreateChannel();
channel.GetData(1);
Console.WriteLine(channel.GetData(1));
Console.ReadLine();
On the client side, we need to have a ServiceContract:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
This ServiceContract is the same as the ServiceContract on the server side.
Because you are calling WCF in core, you need to add the following two packages:
If you use NetTcpBinding, you need to add the following package:
In addition, there are some limitations when calling WCF in core. You can refer to this link:
https://github.com/dotnet/wcf/blob/master/release-notes/SupportedFeatures-v2.1.0.md
Feel free to let me know if the problem persists.
I followed this documentation on setting up a connected wcf service in VS and things worked nicely. https://learn.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide
However, now I'm struggling to figure out how to send this SOAP service my credentials (un, pwd).
Here's what the Connected Service looks like...
https://photos.app.goo.gl/hA6ABN2DNVtXW5KC8
How can I invoke and send my credentials? Then view the result?
You can call server-side methods and properties through the generated proxy class.Here is my demo:
This is a proxy class generated by WCF web service reference provider tool.We need to call services through these classes.
class Program
{
static void Main(string[] args)
{
ServiceReference1.Service1Client service1Client = new ServiceReference1.Service1Client();
System.Threading.Tasks.Task<ServiceReference1.Result> user=service1Client.GetUserDataAsync("Test");
Result rr=user.Result;
Console.WriteLine(JsonConvert.SerializeObject(rr));
Console.ReadLine();
}
}
I create a client through the proxy class and call the service through the created client.
In your code,you also need to create a client like this, and then invoke or send your credentials through the client's method.
Is there some methods to create SOAP-requests and to get SOAP-responses in .net-4.5? Which extentions I should to install, if it's necessary?
You can use SOAP services via the "Add Service Reference" function in Visual Studio. Behind the scenes, this will call svcutil to convert the .wsdl into .cs service prototypes.
The .Net Framework includes both WCF, which is the newer and recommended network communication framework, as well as .Net Remoting, which is more compatible with some non-.Net SOAP endpoints.
See
Introduction to WCF (MSDN)
This answer to a similar question addressing WCF from a SOAP perspective
Walkthrough: Creating and accessing WCF Services (MSDN)
what is WSDL URI in WCF?
Adding Custom [SOAP] MessageHeaders to a WCF Call (MSDN Blog)
Example
For the service located at http://www.webservicex.net/currencyconvertor.asmx?WSDL:
Generate the client proxysvcutil http://www.webservicex.net/currencyconvertor.asmx?WSDL
Rename the config producedmove output.config program.exe.config
Create a test client:
Program.cs:
using System;
using www.webservicex.net;
class Program
{
public static void Main(string[] args)
{
var client = new CurrencyConvertorSoapClient("CurrencyConvertorSoap");
var conv = client.ConversionRate(Currency.USD, Currency.EUR);
Console.WriteLine("Conversion rate from USD to EUR is {0}", conv);
}
}
Compilecsc Program.cs CurrencyConvertor.cs
Run:c:\Drop\soaptest>Program.exe Conversion rate from USD to EUR is 0.7221
I want to make a simple WCF Hello world client which could connect to a WCF REST service.
But I've got the following error:
"Could not find default endpoint element that references contract 'ServiceReference1.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."
What I did:
-I created a new project called "WCFerror" with the "WCF Service Application" template
-My web.config is like this: http://pastebin.com/KEGqRgPr
-My service interface is also simple:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "GetData?value={value}", ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
}
-I created a new Console Application.
-I started a new instance of my WCFerror service (via "Start Debugging"), it is hosted, I tried it out in a web browser
( like: http://localhost:58475/Service1.svc/GetData?value=4 ), it worked fine.
-Then I added a service reference to the Console Application (the address was: http://localhost:58475/Service1.svc) and in the background, the svcutil generated the Client code, and an app.config - but an empty app.config!
-So my client not works:
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
Console.WriteLine(client.GetData(4));
-I tried to run the svcutil via the command prompt like this:
svcutil.exe /language:cs /out:GeneratedProxy.cs /config:app.config http://localhost:58475/Service1.svc
But it generates the same empty app.config.
What did I do wrong? :(
Add Service Reference uses WSDL or WS-MetadataExchange. Both of these are SOAP constructs. REST does not have a metadata standard. You will have to roll the messages yourself, preferably using a framework. Have you looked at HttpClient that is part of the new Web API? Its available via Nuget
I am a little consused with how to acomplish this task. The question is, How can I Call a WCF Services from Multiples Hosted Servers. The WCF is the same for all the Hosted Apps. Same Contract, Same Binding Type, Etc. I am trying to call it in this way because I will host the services in multiples Servers and I need the service to do the same in all of them. I have to call it from one client. VS 2010, .Net Framework 4.0., C#.
Thanks,
It depends how you plan to create service proxy in the client application. If you want to add service reference it is enough to add it from one server and then create separate endpoint configuration for other servers - all endpoints configurations will exactly same except the address (you can do the same in code). When you call services you will create proxy instance for each server and you will pass name of the endpoint (defined in configuration) for each server like:
foreach(var endpointName in myStoredEndpointNames)
{
var proxy = new MyServiceProxy(endpointName);
proxy.CallSomeOperation();
}
Another approach is not using add service reference. In such case you must share contracts between server and client application and you can use ChannelFactory. This class is factory for client proxies which are created by calling CreateChannel. You can pass endpoint configuration name, endpoint address or binding and endpoint address when calling this method.
I use a function like this:
public static MyWcfClientType GetWcFClient(string hostName)
{
MyWcfClientType client = new MyWcfClientType();
// Build a new URI object using the given hostname
UriBuilder uriBld = new UriBuilder(client.Endpoint.Address.Uri);
uriBld.Host = hostName;
// Set a new endpoint address into the client
client.Endpoint.Address = new EndpointAddress(uriBld.ToString());
return client;
}
Of course use your own type for the "MyWcfClientType"