|
-
Apr 24th, 2013, 09:28 PM
#1
Any good examples for WCF and Json?
I have a client that wants WCF in it's pages. Well bull, i have seen 100 different implementations of WCF and i am not sure what to pick. I though good ol web service knowledge would make it easy but it ain't.
So i am looking for examples with the correct implementation of WCF (3.5 framework and above) with usage of jquery-json on the page, with get-post and if possible in VB.
I don't want examples of a stand alone WCF app. It will be used on a web server so net asp.net usage.
Also if i just add a new WCF, will the code that web.config creates be sufficient? Of course changing the DNS location excluded.
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 29th, 2013, 02:56 PM
#2
Re: Any good examples for WCF and Json?
Hey,
Is there a specific reason that they want to use WCF? If so, what are they?
With the release of Web API, if you are just looking to create a JSON endpoint, you might be better to look at that.
 Originally Posted by sapator
Also if i just add a new WCF, will the code that web.config creates be sufficient? Of course changing the DNS location excluded.
Not sure exactly what you mean by this, can you explain further?
Gary
-
Apr 29th, 2013, 06:27 PM
#3
Re: Any good examples for WCF and Json?
Hey Gary, long time no see. 
I am in no mood of using WCF but it's a clients demand so...
WEB API? I must admit i never heard of that. If it's not MVC stuff i would be interested in some explanation on what it does and some examples.
About web.config. If i import the WCF then web.config puts some extra lines in it. So i am not sure if there is a requirement for modifying anything more than the service address.
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 30th, 2013, 01:37 AM
#4
Re: Any good examples for WCF and Json?
Hey,
Web API is a new addition to the ASP.Net Stack, although, if I recall correctly it doesn't "need" ASP.Net to run it, it can be hosted on any OWIN compatible server.
You can find the landing page here:
http://www.asp.net/web-api
At its heart, it is a system which allows you to easily create a "web service" that you can consume in any application. It doesn't have the overhead of WCF. I haven't used it in anger yet, but from what I have heard, it is quite good.
Ah, I see what you mean. You can certainly "tinker" with the server and client settings within the web.config file, but you shouldn't really need to, apart from as you say, the base address. You can even override this in the code behind when you instantiate the new instance of the WCF client, so you don't "have" to do it in the web.config file, but it would make the most sense to do it.
Depending on how much data you are throwing across the system, you might need change some other settings in the web.config file, like MaxBufferSize etc, but you will run into them when you need to.
If you haven't done so already, become friends with two application that ship with the .Net SDK, namely wcftestclient.exe and svcconfigeditor.exe. They are very helpful little applications when working with WCF.
Gary
-
Apr 30th, 2013, 02:26 AM
#5
Re: Any good examples for WCF and Json?
Thanks. I had a look at Web API, show it needed MVC but went ahead anyhow. Then i show some strange MVC syntax, tried a few minutes but gave up calling names...
I am not sure if this can be used without the MVC interaction but it may be.Anyhow will look at it again, someday.
Now on WCF. I am getting tons of problems. I managed to create a WCF service and pass it on json, the problem is that it worked when i got rid the Iservice part completely. Was giving me errors and i saw an example with only one code page...Well it worked.
HOWEVER. I am now trying to use it on a standard windows app. Every single problem i have comes from the web.config. These damn enpoints and what the heck is "mex", i just use "mex" and it works for the web app.
Anyhow my latest exception was about needing a trailpoint or something. I just show that i deleted the win app so i cannot reproduce until i remake the app.
I passed some initial exception by using an extra endpoint with wsHttpBinding instead of webHttpBinding but that was about it.
Here is the WCF part i am using with two different enpoints that supposedly handle both web and winform apps. No i must wonder if i am so stupid that i am constantly cursing Microsoft in my brain and why the hell someone should switch to this BS from simple web services. But they client wants what the client wants.
Here the part from web.config (putted an "x" to distinguish the endpoints). Is it correct (Should i keep IMetadataexcange btw, i was testing something and let it in, but doesn't seem to produce some problem)?
Code:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service name="Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="Service" behaviorConfiguration="ServiceAspNetAjaxBehavior">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="x" binding="wsHttpBinding" contract="Service"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
</system.serviceModel>
P.S. I left the win app app.config as is when adding the WCF to it. Didn't change anything there.
Last edited by sapator; Apr 30th, 2013 at 02:33 AM.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 30th, 2013, 02:38 AM
#6
Re: Any good examples for WCF and Json?
Hey,
Ok, let's take a step back here, as I thinking you might be jumping in too deep, too quickly.
WCF was a re-invention of Web Services, in order to try to account for some of the pitfalls of straight up Web Services. One of the main ones being the lack to "proper" security. WCF was built from the ground up to take this into account, but as a result, it comes along with a lot of configuration. That is where all these endpoint and client configuration settings come from. Looking at this in the web.config file is painful. That is why I suggested you look at the svcconfigeditor, which makes this a little easier to look at.
I think it would make sense to define exactly what you are looking to do.
For instance, from what you said, I am hearing the following...
The WCF Service will be:
1. Housed within IIS
2. Called from an ASP.Net Application
3. Called from a Windows Application
4. Return JSON data to the client
5. Accept JSON data from the client
6. Use .Net Framework 3.5
Have I missed anything?
Gary
-
Apr 30th, 2013, 02:52 AM
#7
Re: Any good examples for WCF and Json?
I wouldn't mind configuring the web.config myself( good ol' asp.net habit) as long as i knew what these endpoints and mex-es do exactly. Yes i went deep at once, you are correct but i think i understand most of it except maybe the mex and the IMetadataExchange.
Anyhow, your 6 points are correct and i would add some more.
7)Accept data from win app (probably a dataset)
8)Return data to win app
9)Using 3.5 framework is not a problem, i can go as high as 4.5 framework.
I think i have managed 1. 2. 4. 9. but with the warning that i completely deleted the Iservice.vb code page.
Unfortunately now that i have found you on-line i must leave for a job but i will take a look at any suggestions tonight.
P.S. Would also be nice to see a security example.Just saying because you mentioned it and i was not using any security log in at all but i will have a look at this also.
Thank Gary i'll see any suggestions later on. Will try to reproduce the error i got tonight and be more specific as i would be in my home PC.
Cheers.
P.S. The WCF is also called from the winforms app ok , but when i try to request some data then it give the exceptions.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 30th, 2013, 03:11 AM
#8
Re: Any good examples for WCF and Json?
Hey,
There is nothing to stop the Windows Application consume and return JSON data as well, there is no need to switch to using a DataSet.
In terms of security, you have the standard options, you can use Windows Authentication for example. If you are going to go up to .Net 4.5 you then have the option of using Claims Authentication, but it really depends on how far you want to go.
Why did you delete the IService? This will likely contain the ServiceContract and OperationContract that defines the WCF Service. You kind of need that 
In terms of the endpoint that you will want to use, the straight up wsHttpBinding is the one that you will likely be using. The MEX endpoint is really just for getting the meta data about the endpoint, i.e. what methods it exposes etc. When you actually interact with the service, you won't be using it.
Gary
-
Apr 30th, 2013, 08:49 PM
#9
Re: Any good examples for WCF and Json?
Ok.Could not combine both asp.net and winforms to call a WCF yet. Lots of errors.
I managed to call one WCF for asp.net and one WCF for winapp. It work with the standard config (the winapp service) aka i do not touch anything.
Let me show what i did for the winapp and ask a few questions.
Iservice:
Code:
Public Interface IService1
<OperationContract()>
Function GetData(ByVal value As Integer) As String
<OperationContract()>
Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType
' TODO: Add your service operations here
<OperationContract()>
Function getdocs(ByVal strdoc As String) As DataSet
End Interface
<DataContract()>
Public Class CompositeType
<DataMember()>
Public Property BoolValue() As Boolean
<DataMember()>
Public Property StringValue() As String
End Class
service the function:
Code:
Public Function getdocs(ByVal strdoc As String) As DataSet Implements IService1.getdocs
Dim strConnString As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
'never put the text like this, this is just a test!!
cmd.CommandText = "select [name] from doctors where " +
"[name] like @prefix + '%' "
cmd.Parameters.AddWithValue("@prefix", strdoc)
con.Open()
Dim sda As New SqlDataAdapter
sda.SelectCommand = cmd
Dim ds As New DataSet
sda.Fill(ds)
con.Close()
Return ds
End Function
web config on WCF did not touch anything, just inserted the connectionstring...
Winapp:
did not touch anything at the connection string.
Button (async(?)
Code:
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ct As New ServiceReference1.CompositeType
ct.StringValue =TextBox1.Text
Dim mysrevr As New ServiceReference1.Service1Client
Dim ds As New DataSet
'ds = mysrevr.getdocs(TextBox1.Text)
ds = Await mysrevr.getdocsAsync(TextBox1.Text)
DataGridView1.DataSource = ds.Tables(0)
End Sub
Now this works ok but as i've said i can't combine both asp and winapp, it goes into ultradisaster but i guess i managed to run the damn thing for now..
Questions:
1)How exactly i will set this up to IIS? Just add a new website and bind to the WCF service?
2)What is the use of CompositeType? As you can see above i did some testing with CompositeType but i then dropped it to use a single string value.
3)Is there any example of adding security to WCF. Ok windows authentication but how will i implement it here?
4)I would be interested in an example of de-serialazing json to a winapp. This is not a requirement from the client but it's for my interest.I am thinking i have to mess with web.config to make this work and i am siting in my corner, afraid with cold sweat and some tears coming out from my eyes.
5)Private Async Sub Button1_Click. This is supposed to call the data asynchronous?I am right?Haven't seen an asynch button,first time, so it seems obvious but i am not sure...
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
May 1st, 2013, 01:09 AM
#10
Re: Any good examples for WCF and Json?
Hey,
Are you working from someone else's sample here? If so, can you provide a link? You seem to be asking questions about code that I would have thought you had written, but perhaps you are just using existing code.
1. Essentially, yes. Have you created a WCF Service Library? If so, it's just a case of creating an .svc file, with the ServiceHost definition contained within it, pointing at your service, and away you go.
2. Please see the comment above.
3. This is set up within the Binding for the WCF Service Endpoint. You can see an example here: http://www.codeproject.com/Articles/...tion-on-WCF-Ba
4. There are numerous libraries out there for serializing and deserializaing JSON to .Net Types. For instance http://james.newtonking.com/projects/json-net.aspx. Have a look here for an interesting take on it: http://jonas.follesoe.no/2008/03/04/...ns-using-json/
5. This is using the new Async/Await pattern with now comes in .Net Framework 4.5. This is a whole other topic in itself, which we can talk about, however, if you are going to stick with .Net 3.5, this won't be an option for you.
Is it possible that you can send me what you have? I can take a look if I get a second.
Gary
-
May 1st, 2013, 01:47 AM
#11
Re: Any good examples for WCF and Json?
Hey, about question 2. This is what VS puts by default on the service so i was trying to understand what was going on.
I don't have a specific sample that i work with, just some bit here and there but anyway i usually don't follow a sample because it does the work for me and then i haven't understand completely what i did.
Thanks for the samples, i had a quick look at authentication but on Step 5 it says about anonymous access. This is completely different in IIS7.5 but i guess it's authorization rules and again i usually play with web.config so i can't be sure on what exactly should enable or disable on IIS.
I will also mark the json examples to have a look.
I won't stick with 3.5, i mean for my projects, yes 3.5 is nice but the client of this project doesn't mind using 40. or 4.5 , anyhow does it work using this async feature, or it's like the updatepanel async? 
P.S. I will wrap the samples for you and send them over in a sec.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
May 1st, 2013, 01:58 AM
#12
Re: Any good examples for WCF and Json?
Hey,
With regard to async/await, it is basically some syntaxic sugar around the standard Async delegates and callbacks that we used to have write. Now you can use the "await" keyword to essentially "wait" on calling a method, whch returns at some point in the future. Execution of the code will continue, therefore not block the UI thread, and will return to the await keyword when complete. This is the new pattern, and the way a lot of things are going. If you haven't had a chance to look at async/await yet, I would strongly suggest that you take a look. It is really quite cool!
Cool. I will take a look when I can.
Gary
-
May 1st, 2013, 02:11 AM
#13
Re: Any good examples for WCF and Json?
Yes i will have a look probably next week, will be an async look, will read and then watch TV and when my brain compiles and return i will go to the PC :P . Today i am leaving for Eastern so there is no rush on looking at the link. I don't think it's a big deal anyhow,in the way that it's a very simple WCF service. I guess the real problem will be when i start messing around with web.config.
Thanks for the suggestions.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|