Is there a way to read the web service methods dynamically using a program? I have a windows forms app that should be able to read the list of methods and display them. I have added a service reference to my project but need help to read the list of web methods or operations (WCF).
Answer:
Here is the piece of code just in case anyone is looking for it.
MethodInfo[] methods = typeof(MyClass).GetMethods(BindingFlags.Public | BindingFlags.Instance);
if (methods != null && methods.Length > 0)
{
foreach (MethodInfo m in methods)
{
foreach (object o in m.GetCustomAttributes(false))
{
// To identify the method
if (o.GetType().Name.Equals("SoapDocumentMethodAttribute"))
{
// Get Name using m.Name
}
}
}
}
Alternatively, if you need to read the methods of a service on-the-fly, this article may be of interest to you, as it illustrates how to create a WCF proxy from WSDL.
http://blogs.msdn.com/b/vipulmodi/archive/2008/10/16/dynamic-proxy-and-memory-footprint.aspx
Then you can use reflection (as per Mike's suggestion) to read the list of service methods exposed by the service.
On your client side, since you already have a web reference type for the web service, you can just use reflection to list all the methods in the proxy client class.
Related
I have an asmx web service I am converting to WCF. In the old structure I had a number of web service methods that accessed business classes to do the work. For example:
public DataSet GetAppointmentDataToConvert(string aAccessToken, string aStartingId)
{
//verify that the user is allowed to use this web service
if (!isAuthorized(aAccessToken))
{
throw new Exception("Not authorized");
}
cls256_BitEncryptionConversion cls256 = new cls256_BitEncryptionConversion();
return cls256.GetAppointmentDataToConvert(aStartingId);
}
This method initialized the cls256_BitEncryptionConversion class and called a method inside it to do the work, then returned the result.
I am trying to set up a WCF Service that will use the cls256_BitEncryptionConversion class, but despite both the class and the service being in the same namespace, my service method is unable to "see" the class. What am I doing wrong?
These are few tips for you to resolve it your self:
Make it sure that even they have the same namespace are they in the same project?
If they are in different project add it as you reference where you want to use it. And probably add a using directive if necessary.
I was able to resolve the issue by restructuring the project. I moved all the classes from the app_code folder into the root of the project.
I used to code only in databases enviroments. Recent changes in the corp. made me start developing in whole new worlds.
This new project is something like SQL - C# - PHP.
The class I've been working on in VS2008 is almost dnoe. It calls all the SQL SPs I need and the info is there in the DataReaders.
When It came to read that info from PHP so I could populate the website I found out it wasn't that easy. I was recommended into trying several options, the one that suits the best for the project is to create a Web Service and then consume it (please be patient, As I just stated I'm new to most web related programming)
So, I'm trying to WCF/Rest and then consume it from PHP but I haven't got there yet.
I've read and watched several tutorials on WCF and It seems to be smooth, but all I've read is:
. Create Interface with its OperationContracts.
. Create Service with DataMembers etc and define the Methods listed in the Interface.
Ok, but what I'd like to do is not to specify any methods there, since all I want is to call C# method I've already written.
Should I do that in the Service or in the Interface? And first of all, is this the right the way to approach it?
You would want to write service methods that implement an operation contract interface. The service methods can call the C# code that you've already written.
For example, here is a simple service interface:
[ServiceContract]
public interface IYourService
{
[OperationContract]
int GetCountOfTransactions(string filter);
}
And then you would implement this interface in your service class:
public class YourService : IYourService
{
public int GetCountOfTransactions(string filter)
{
// Call your existing code
YourClass yourClass = new YourClass();
return yourClass.GetCountOfTransactions(filter);
}
}
There are plenty of examples out there for setting this up as a REST service, but I think you're on the right track.
The trickiest part is usually setting up the binding configuration to make sure all of your consuming client applications can connect.
Hopefully this helps.
I'm new to workflow 4.0 and WCF and am struggling to create a WCF service that creates, runs and communicates with Workflows of different types.
Using workflow 3.5 I was able to create a nice webservice that creates and destroys workflows of different types, and allows a single interface to talk to workflows of varying types. For example, to create a workflow of a given code in 3.5: (where a code corresponds to a workflow type)
[WebMethod]
public string CreateWorkflow(int code)
{
WorkflowRuntime runtime = createRuntime();
try
{
Type type = GetWorkflowType(code);
string reply = "";
WorkflowInstance instance =
runtime.CreateWorkflow(type);
instance.Start();
reply = instance.InstanceId.ToString();
instance.Unload();
return reply;
}
catch (Exception e)
{
return e.ToString();
}
finally
{
closeRuntime(runtime);
}
}
And then to communicate to a workflow of any type:
..
WorkflowInstance instance = runtime.GetWorkflow(new Guid(Id));
PassDataEventArgs eve = new PassDataEventArgs(instance.InstanceId, data);
InputData(null, eve);
..
Is it possible to present such an interface using WCF and workflow 4.0 ? I've managed to create workflows of different types using WCF, but to communicate with them i'm not sure how to pass data along. Ideally invoking a method like passData(Guid id, Object data) on my WCF service would pass this data along to the instance of a workflow of any type (catching an error if the workflow is not in the correct state to recieve data at that time). Can anyone please give me any insights as to how I could achieve such generic communications across multiple workflow types inside a single WCF service?
IN WF4 the usual way to run a workflow as a service is to use the WorkflowServiceHost. This is however restricted to a single workflow definition per WorkflowServiceHost. If you want multiple workflows you would use multiple WorkflowServiceHost's.
Now you can create a generic service that would accept all messages, inspect them and based in some criteria pass them on to the correct WorkflowServiceHost. An alternative is not to use the WorkflowServiceHost but use WorkflowApplication instead but that would mean writing a lot of plumbing code.
I have an object of the form:
public class Report
{
m_filter1;
m_filter2;
...
m_filtern;
AddFilter1(int a)
{
m_filter1 = /* some logic of filtering results using the value of a */
}
...
}
Also, a corresponding static method used to utilize the Report class:
public static List<Result> GetResults(Report r)
{
/* use r to query the DB and return an array of results */
}
As this method needs to be exposed using WCF, i must also make the class Report available for 'outside' (client) use, but its member-methods, which hide the internal plumbings, cannot be used in the WCF proxy class generated for it.
As i can't expose the private members of Report, how can i elegantly solve the problem of member-methods needed on the consuming side?
I thought of a service contract of the form:
public class ReportingService
{
Report m_report = new Report();
AddFilter1(int a)
{
m_report.AddFilter1(a);
}
...
}
i.e., to wrap the member-methods of the Report class using a single Report instance - but that puts a limitation of using a single, non thread-safe object shared by all the calls to the service.
Is there anything basic i'm missing here? i'm quite new to WCF, so i've probably overlooked an easy pattern of solving this out.
Thanks,
Harel
Well, as you've noticed - WCF only ever conveys data across the server-client link - and that's absolutely intentional. WCF handles serialized messages - data only. Think about it for a second: WCF is totally interoperable - your client could be a PHP site calling you - how would those guys be able to execute your .NET code??
So basically the design recommendation is: make sure your data contracts are just that - pure data, no behaviors, no methods, nothing like that.
If you need to do something on a piece of data - define a service method for it! That's what the whole service-oriented architecture is all about.
So basically: there's really no elegant or proper way to achieve what you want - except for making your methods into service methods that operate on simple data contracts.
Adding a service reference to a web service (this is all WCF) in Visual Studio produces some generated code including a client-side restatement of the interface being exposed.
I understand why this interface is generated: you might be consuming a 3rd party service and not have access to the actual interface.
But I do, and the two are not assignment compatible even though the transparent proxy does indeed exactly implement the interface to which I want to cast.
I can use reflection, but that's ugly. Is there some way to defeat this faux type safety and inject metadata to so I can use an interface with a class?
My specific problem departs from the norm in complicated ways that have to do with a single client that uses some derivatives of a base class directly and uses others remotely via service references. The base class for each server needs to keep references to subscribing clients in a collection for enumeration to notify events, and the problem was type varied due to the use of proxies.
None of these answers solves my specific problem, yet every single answer was instructive and helpful. I found my own solution (use a dual binding) but I would never have figured it out if you hadn't radically improved my understanding of the whole business.
Three excellent answers. How to choose just one? I choose the first, because it directly solves the problem I first thought I had.
If you already have the contract dll at the client, you don't even need a service reference (unless you are using it to write the setup code for you) - you can simply subclass ClientBase and expose the Channel, and use that directly - something like (no IDE handy...):
public class WcfClient<T> : ClientBase<T> where T : class
{
public new T Channel {get {return base.Channel;}}
}
Then you can just do things like:
using(var client = new WcfClient<IFoo>())
{
client.Channel.Bar(); // defined by IFoo
}
You still need the configuration settings in the config to determine the address, binding, etc - but less messy than proxy generation. Also, you might choose to re-implement IDipsoable to deal with the fact that WCF proxies can throw in Dispose() (which is bad):
public class WcfClient<T> : ClientBase<T>, IDisposable where T : class
{
public new T Channel {get {return base.Channel;}}
void IDisposable.Dispose() {
try {
switch(State) {
case CommunicationState.Open: Close(); break;
// etc
}
} catch {} // swallow it down (perhaps log it first)
}
}
When you add the service reference, go to "Advanced" and make sure "Reuse types in referenced assemblies" is selected and that the assembly containing your interface definition is selected. You can also do this with an existing service reference by right clicking on it and going to "Configure".
In order to return an interface from a service you need to use the KnownType attribute:
http://weblogs.asp.net/avnerk/archive/2006/07/31/WCF-Serialization-part-1_3A00_-Interfaces_2C00_-Base-classes-and-the-NetDataContractFormatSerializer.aspx
If you want to return a custom type from the service:
http://msdn.microsoft.com/en-us/library/bb628653.aspx
Does any of that help?