Trying to send a complex type between two systems that have the same code base. They are both aware of the same object that is the data transport medium. However when I pass object on the consumer proxy it appears as an object in the web service namespace instead of the one in the application.
Is there anyway that I can define to use the internal object?
[WebMethod]
public void Run(TransportObject transport)
appears in my application under MyNamespace.Webservice.Transport instead of MyNamespace.Objects
As Henk says sharing of types in WCF is default behavior.
I will qualify that by expanding on it and demonstrating how to achieve it:
So rather than have visual studio generate a service reference for you, you can have WCF create the proxy when it executes by using ChannelFactory.
For example:
// Create service proxy on the fly
var factory = new ChannelFactory<IMyServiceContract>("NameOfMyClientEndpointInConfigFile");
var proxy = factory.CreateChannel();
// Create data contract
var requestDataContract = new MyRequestType();
// Call service operation.
MyResponseType responseDataContract = proxy.MyServiceOperation(requestDataContract);
In the above example, IMyServiceContract is your service contract, and MyRequestType and MyResponseType are your data contracts, which you can use by referencing the assembly which the service also references (which defines these types).
[WebMethod] implies it's an ASMX based service, not a WCF service...
Try again with an [OperationContract] in a [ServiceContract]
Sharing of Types in WCF is possible, I think it's even the default behaviour.
Related
I am consuming 2 WCF service in 1 WCF service.
using WcfService.Server1;
using WcfService.Server2;
I am calling a function that is returning endpoint name of suitable WCF service based on some logic. How to create object of WCF service based on endpoint name dynamically?
In below code I created WCF object of "Server1" WCF service but suppose method returns endpoint name "Server2Endpoint" then it would be a problem because I am referring WcfService.Server1
WcfService.Server1.MatrixCalculationClient Proxy1 = new WcfService.Server1.MatrixCalculationClient("Server1Endpoint");
I assume that both WCF services implements same interface, e.g. IWCFService.
Then I believe you will have to use either your own proxy class (which implements IWCFService independently of Server1 or Server2) or construct channel manually using ChannelFactory<IWCFService>.
In both cases you can pass name of the endpoint to the proper constructors and then everything will work if app.config file contains correct definition of these endpoints (same contract).
I am writing a basic WPF GUI to connect to a WCF service and consume an interface. So far I have connected to the test system by creating a service reference, putting in the URI for the test service I want to consume, it finds the interface and creates the proxy via service reference for me.
What I want this to do when you run the GUI app is for the user to be able to pick an environment - development, test or production and for the GUI to then connect to the appropriate WCF service depending on the environment selected.
How can I do this?
You can overwrite the Endpoint like this:
client.Endpoint.Address = new EndpointAddress(GetAddressForCurrentMode())
The other way you could to it, is to write a method, maybe an extension method, that accepts the service contract and the implementation class. Further more it either accepts a configuration name, or an endpoint:
public static TClient GetServiceClient<TClient, TContract>(string endpoint)
where TClient : ClientBase<TContract>
{
// Construct client
}
To construct the client, use one of BaseClient<T> overloads (from MSDN).
To then consume the client, just use the method above as normal:
using(var client = ServiceInterop.
GetServiceClient<MyClient, IMyContract>("http://foo.bar"))
{
// Consume client
}
I am currently developing a WCF client to interaction with a set of WCF service references. I am developing in c#.
I don't want to following the approach of having to generate a service reference using svc util or manually adding a service reference to my class library.
Have I an other alternatives open to me? I am considering using the ServiceClient class within the ServiceModel library.
I am little confused though, for example the request and response objects related to an endpoint, where are these created or how are they created? In a previous project I used T4 mappings and DTO's, but I feel these are over kill. I did like though that I could share the same object between different service endpoints. My goal here is to create a custom client object communicating through a custom written proxy. I would like some direction on this.
To talk to a WCF service (endpoint) you need to know three things (ABC): the address of the endpoint, the binding it uses, and the contract used in the communication. If you have all those three things, you don't need to use any tool to interact with the service.
The address is just the URI of the endpoint. The binding is represented by one instance of the abstract System.ServiceModel.Channels.Binding class (such as System.ServiceModel.BasicHttpBinding, System.ServiceModel.WSHttpBinding and so on). And the contract is usually represented by an interface decorated with the [ServiceContract] attribute. If you have all those three, you can create a custom proxy by using the ChannelFactory<T> class, as shown below.
public static void TalkToService(Binding binding, Uri endpointAddress) {
// Assuming that the service contract interface is represented by ICalculator
var factory = new ChannelFactory<ICalculator>(binding, new EndpointAddress(endpointAddress));
ICalculator proxy = factory.CreateChannel();
Console.WriteLine(proxy.Multiply(45, 56));
}
The ONLY argument I can see for SOAP WCF over REST (json) wcf is the fact that once my service is created I can add a a reference in visual studio and I get a load of strongly typed classes ready for me and a client class that I can call all my webmethod through. It even sets up the web.config as far as I remember.
However when I expose a REST (json) service I still get a WSDL. So Im wondering is there still a way to build my references automatically?
Not using WCF tools. Unlike with SOAP (which has an established protocol for describing services - WSDL), REST doesn't. WADL is one such protocol, but it isn't too widespread and WCF does not support it. You still get a WSDL, because WCF will describe everything it can from the service. However, the WSDL won't have a <wsdl:port> element, which would describe the REST endpoint, which is why you get the WSDL, but cannot generate a reference to it.
The post at http://blogs.msdn.com/b/carlosfigueira/archive/2012/03/26/mixing-add-service-reference-and-wcf-web-http-a-k-a-rest-endpoint-does-not-work.aspx has a lot more info on this issue.
Very old question, newer answer.
today using openapi (swagger) I can achieve this by using swagger inspector doing samples i can document my rest services as well as create a spec yml/json file allowing for validations and acceptance criteria as well as automated clients for java,python,c#,ruby,javascript and others I'm sure
I would like top elaborate:
Although it is true you cannot get a WSDL add service reference with a JSON REST WCF service, what I do is create two met data hooks:
is the operations returning JSON
is a single XML op returning a class wrapper which includes all the service classes I allow, I call it Discover:
i.e.
public class Discover
{
public Manager Manager {get;}
public Employee Emp {get;}
....
}
[OperationContract]
public Discover DiscoverDTOs()
You can, indirectly. While the client generated by Visual Studio won't work, that client implements an interface, also generated, that you can use like this:
WebChannelFactory<IService> factory = new WebChannelFactory<IService>(new Uri(endpointAddress));
IService proxy = factory.CreateChannel();
int result = proxy.Operation(1, 2, 3);
WebChannelFactory has another overload which accepts a WebHttpBinding, you can configure based on the service configuration, or you can make this configuration manually in your app.config file.
There is a web service written with WCF that I'm adding as reference. Proxy class generator works flawlessly when I add it to as service reference but it generates a slightly different class when I add it as web reference using service.svc?WSDL. Here's are the differences:
//service reference
public partial class TestServicesClient : ... // correct class name
{
public int TestMethod(string serviceID, int dealID) // correct method signature
{ ... }
}
//web reference
public partial class TestServices: ... //different class name
{
public void TestMethod(string serviceID, int dealID, bool dealIDSpecified, out int TestMethodResult, out bool TestMethodResultSpecified) // different method signature
{ ... }
}
I tried using wsdl.exe for generating web reference class, didn't help.
What is wrong here?
Nothing is wrong here. Service Reference is the "new" way to use a WCF Service, it removes overhead such as the "Specified"-parameter, "Result"-parameter and "Result Specificed"-parameter.
You can still use other properties/methods to check if a parameter is specified or if there is a result. But before WCF, it changed the method signature.
You use a Service Reference and a Web Reference a bit different and that's just the way it is.
Here's some additional reading:
Difference between web reference and service reference?
The Difference Between “Add Web Reference” and “Add Service Reference”
And to quote from a reply to "What is the difference between WCF service and web service"
WCF "web services" are part of a much
broader spectrum of remote
communication enabled through WCF. You
will get a much higher degree of
flexibility and portability doing
things in WCF than through traditional
ASMX because WCF is designed, from the
ground up, to summarize all of the
different distributed programming
infrastructures offered by MS. An
endpoint in WCF can be communicated
with just as easily over SOAP/XML as
it can over TCP/binary and to change
this medium is simply a configuration
file mod. In theory this reduces the
amount of new code needed when porting
or changing business needs, targets,
etc.
ASMX is older than WCF, and anything
ASMX can do so can WCF (and more).
Basically you can see WCF as trying to
logically group together all the
different ways of getting two apps to
communicate in the world of MS; ASMX
was just one of these many ways and so
is now grouped under the WCF umbrella
of capabilities.
There are few difference between the service reference and web reference. WCF serializes using datacontract serializer. So add XmlSerializeFormat attribute to the serivcecontract and then add web reference. Your extra parameters will be removed. But then its like using wcf like web service only. One more retriction is that you need to use web bindings only like http.