0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot Garret! It's helping me already. If I want the method GetCountOfTransactions to return an Object (one of my C# class) I'd have to reference the class into the WCF WS project and that's it right?
Right, just add a reference to your library or project with your class, and that should do it.
Everything seems to work but this! Hope you can enlighten me once more so I can finish this up. When in the service class I write "return yourClass.GetCountOfTransactions(filter);" after yourClass. I can't see the methods but just the properties. So I can't GetCountOfTransactions after yourClass. The class definition is in one project, and the methods are in another (there's a reference created in the method's class to the class' project) Any tip? Sorry for the hussle, and thanks once more.
Not exactly sure, but just make sure that in your WCF project you're referencing the project that includes the methods. Good luck with everything.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.