Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Thursday, March 31, 2011

WCF RIA Services Compositions with Entity Framework

I haven't been working on anything outside Genexus, Deklarit and Genexus Server for a while, so when my friend Mateo asked to help him on a new project for a client of his, based on brand new Microsoft technologies, I was saying yes before the end of the sentence.

This blog post and probably some more to come will be related to our experience with Entity Framework 4, RIA Services 1 and Silverlight 4.

But in this particular case I wanted to blog about a problem we had with Compositions. Compositions are very useful when you have an Association where a Parent entity needs to have its children all the time.

The Update method of parent entities is a bit different than regular entities. I took the patter from this article (Compositional Hierarchies) but things didn’t work as expected. I have to add that “m generating POCO entities and using the Unit Of Work pattern so it wasn’t easy just copy and paste the code for the article. I posted a few questions to the RIA services forum and found out that that pattern exposed a bug :(

Fortunately I was redirected this post from Brett Samblament which described the new pattern to follow to write a fully functional UpdateParent method. But again, I can’t just copy and paste, so here’s the code I wrote for it. Also, by using generic, I’m able to call the exact pattern for every composition in my model, cool uh?!

public void UpdateParent(Parent parent)
{
EntityHelper.UpdateParentEntity(parent, ObjectContext.Parents, ChangeSet, ObjectContext);

foreach (Child child in ChangeSet.GetAssociatedChanges(parent, o => o.Children))
EntityHelper.UpdateChildEntity(child, ObjectContext.Children, ChangeSet, ObjectContext);

}


And my EntityHelper class has the following methods:



public static void UpdateParentEntity<T>(T entity, IRepository<T> repository, ChangeSet chgSet, IUnitOfWork oc)
where T : class
{
try
{
ObjectContext ctx = oc as ObjectContext;
repository.ObjectSet.AddObject(entity);

T originalEntity = chgSet.GetOriginal<T>(entity);

if (originalEntity == null)
ctx.ObjectStateManager.ChangeObjectState(entity, EntityState.Unchanged);
else
repository.ObjectSet.AttachAsModified(entity, originalEntity);
}
catch (Exception ex)
{
TraceManager.Error(string.Format("An error occurred updating a {0}", typeof(T)), ex);
throw;
}
}


public static void UpdateChildEntity<T>(T entity, IRepository<T> repository, ChangeSet chgSet, IUnitOfWork oc)
where T : class
{
try
{
ObjectContext ctx = oc as ObjectContext;
ChangeOperation change = chgSet.GetChangeOperation(entity);

switch (change)
{
case ChangeOperation.Delete:
if (GetEntityState(entity, ctx) == EntityState.Detached)
repository.ObjectSet.Attach(entity);
ctx.DeleteObject(entity);
break;
case ChangeOperation.Insert:
// do nothing
break;
case ChangeOperation.None:
ctx.ObjectStateManager.ChangeObjectState(entity, EntityState.Unchanged);
break;
case ChangeOperation.Update:
T original = chgSet.GetOriginal<T>(entity);
if (original == null) { throw new Exception("Update with no original value found"); }
if (GetEntityState(entity, ctx) == EntityState.Detached)
repository.ObjectSet.Attach(entity);
repository.ObjectSet.AttachAsModified(entity, original);
break;
default:
break;
}
}
catch (Exception ex)
{
TraceManager.Error(string.Format("An error occurred updating a {0}", typeof(T)), ex);
throw;
}
}


public static EntityState GetEntityState(object entity, ObjectContext ctx)
{
System.Data.Objects.ObjectStateEntry ose;
if (ctx.ObjectStateManager.TryGetObjectStateEntry(entity, out ose))
return ose.State;
else
return
EntityState.Detached;
}


I hope this helps someone clear the way… and I promise I’ll post more (and more often) about this.



As usual, this code is ‘works on my machine’ certified.



works on my machine



I never get tired of this Smile

Read Full Post

Friday, August 06, 2010

Annoy Smarx from Windows Phone 7

Annoy Smarx is a web app that Steve Marx showed in Windows Azure Firestarter event. It is a web app that once you clicked on an image, Steve’s notebook wallpaper would change to the selected image. It was a pretty cool demo cause it showed you a good practical sample of AppFabric ServiceBus.

Last week I started playing around with the new Windows Phone 7 SDK, and I faced the problem I do when I start something new, “what should I code”? I t has to be simple cause I’m new to this stuff (I’m no too familiar with silverlight either) but not too simple… I hate the “Hello World!” sample. So I thought maybe I can create a client for the AnnoySmarx sample and so I did.

I liked it cause it a simple project, yet it has many cool features, like downloading blobs from Azure Storage, using a ListBox to show the images, handling the double tap event on Windows Phone 7 (there’s no OnDoubleTap event, so it might be a little more complicated than you think), and calling a rest service on ServiceBus which is actually running on my computer.

You’ll find all the resources you need at Steve’s post and the code for this client here.

I recorded a video of the app running on the emulator…

pretty cool uh?!


works-on-my-machine-starburst_3_thumb[1]

Read Full Post

Tuesday, June 22, 2010

HTTP Error 404.17 - Not Found

The requested content appears to be script and will not be served by the static file handler.

For some (unknown at the time) reason I had this error while trying to view a WCF service app in the web browser. I googled (and binged) around and there was many reasons for that message.

My problem was that my brand new computer came with Microsoft .net 3.0 installed but IIS was not active. So I installed IIS after the WCF support so the WCF mime types were not installed.

To get this working all I had to do is running the following command:

C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg -i

Hope it helps someone else.

Read Full Post

Tuesday, October 27, 2009

Internal Error 500 returned by IIS7 hosted WCF services

500 Here's an issue I had for quite a long time.
For some (unknown at the time) reason the server was returning a "500 - Internal Server error" whenever an exception was thrown.
Even the message on the exception was an html document (pasted below), so who was modifying my exceptions? it turned out to be the IIS itself due to some configuration settings.
There's a "feature" which avoids the IIS to send the real exception to the client sending instead a more "friendly" one.
So, how do you change that? (keep in mind this is IIS 7.0 and above)
Take a look at this great post (where I got the solution from) and follow step 2) of "Enable IIS7 detailed errors".

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<
title>500 - Internal server error.</title>
<
style type="text/css">
<!--
body
{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</
head>
<
body>
<
div id="header"><h1>Server Error</h1></div>
<
div id="content">
<
div class="content-container"><fieldset>
<
h2>500 - Internal server error.</h2>
<
h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
</
fieldset></div>
</
div>
</
body>
</
html>

Read Full Post

Friday, September 04, 2009

Microsoft .net Framework 2.0 Configuration missing?

SDKAfter my Windows7 install I needed to modify some of the machine.config properties thru the .net configuration utility and I could find it. It wasn’t where it used to be (Administrative Tools) and even the Snap-in on the MMC was not present.

After a little bit of search I realize I had to install the .net framework 2.0 SDK. I guess I did it a long time ago cause I don’t remember even doing it.

Here are the links.

http://www.microsoft.com/downloads/details.aspx?FamilyID=fe6f2099-b7b4-4f47-a244-c96d69c35dec&displaylang=en (32bits)

http://www.microsoft.com/downloads/details.aspx?familyid=1AEF6FCE-6E06-4B66-AFE4-9AAD3C835D3D&displaylang=en (64 bits)

We’re welcome :)

P.S: I needed it in order to deploy a web app to the Azure platform, that’s why I’m tagging this post as “cloud computing” related

Read Full Post

Thursday, March 19, 2009

log4net with WCF Services

This week I ‘ve been working on adding tracing to a WCF IIS hosted application. It all started pretty easy since log4.net already has an appender for asp.net.

So, I created a little wrapper around the log4net library and called the Debug function. My project had both aspx pages and svc services. These services were implemented in a separate dll (not in the web project). Oh! I forgot to mention that I wanted to use the AspNetRaceAppender so I could watch the trace in the trace.axd ‘page’.

But it was not working… I had a few debug entries and nothing appeared on the trace page. The ‘wired’ part was that if I’d copied and pasted the same debugging line in the aspx code behind’s it’d worked! So what’s up with that?

I started googling around I found close to nothing about log4net and WCF services. And that’s why I decided to post the solution here. Trying around many different scenarios I found the solution. You have to enable your wcf services to get the HttpCopntext from the web application. And in order to do that, you need to add the following line in your web app’s web.config file.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />


Add that line inside the system.serviceModel part. Also, you have to add the following attribute to your services’s classes.



[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]


Hope this helps somebody!



P.S: All the code I write is “works on my machine” certified ;)



works-on-my-machine-starburst_3

Read Full Post

Sunday, August 03, 2008

Next stop: Florida!

dklogo

I didn’t want to say anything but a few days ago Milano mentioned it in a post in our forum so I guess that gave me the green flag :)

We are now working on the next version of DeKlarit code-named “florida”. We want to add a lot of new stuff but I think one of the most important features will be the Silverlight addin which will let you create web applications with Silverlight controls. We are still in the process of making a few decisions like, will it be a full Silverlight app or will it have aspx pages as containers for the Silverlight controls? We believe the second is the best choice right now since we have two excellent web addins and we can use the navigation logic we already have in those. That means it will be pretty easy for you to migrate your current WebGenerator generated app to a brand new Silverlight app. Cool uh?

Also, we’re thinking on REST support for our WCF addin, WPF for a fully funtional WinGenerator addin independent of third party controls, something you guys’ve been asking for, and full text search in generated applications among others. Also, I have to work on our security features for the generated apps and add a few features like allowing a user to change his password, sending conformation emails and more.

So if you have any suggestions, now is the time. I can’t assure you we’ll implement them all but we’ll surely keep’em under the radar.

 

Read the full thread of the “announcement” here.

Read Full Post

Friday, August 01, 2008

Visual Studio 2005 Extensions for WCF and WPF not available after installing Visual Studio 2008

WCF It’s a fact, Microsoft wants you to use the 2008 version of Visual Studio rather than 2005. But what if you are in a big project, not ready to change the IDE, yet, and you want to install VS2008 to test the new features? They say everything works fine but the true is you loose an important extension: Visual Studio 2005 extensions for .NET Framework 3.0 (WCF & WPF).

The problem is that you can’t install them because you have .net 3.5 installed so googling around I found a pretty useful post from Timur.

Just locate the installer with your Visual Studio 2005 command line and execute the following command:
msiexec /i vsextwfx.msi WRC_INSTALLED_OVERRIDE=1

I don’t know what’s going on with that package cause the site where it used to be available to download from is no longer available, so if you need it I uploaded to my SkyDrive account. Download it from here.

Edit: I also found it here, as I mentioned in my comment below.

Read Full Post

Friday, February 08, 2008

The truth is out there...

...and so is DeKlarit 4.5

We have just released DeKlarit 4.5 for 2005 and 4.5 for 2008.
Both versions share the same functionality, but this is our last version supporting VS2005, so start your migration right away. I know I said before that you should create a blank VS2008 project with DeKlarit and import your existing objects to it. Now all that changed, I mean, you can still do it that way, but now I believe the best way is to just open your existing project with VS2008. You will have to add the addins back again.

Remember different VS versions of DeKlarit can coexist. Which means 4.5 for VS2008 can be installed in the same box where 4.5 for VS2005 or 4.3 is. But to install 4.5 for VS2005 you have to uninstall 4.3

There are some cool new features like a suggest combo in foreign keys in the WebGeneratorInfragistics Addin, full WCF support, extension capabilities for the generated application menu, and many more.

Give it a try!, and let us know what you think.

Read Full Post

Saturday, December 15, 2007

WCF Membership Provider sample

I worked so hard on this that I thought I might share the final product with everybody.

Start here and download Michele's book's Appendix A, I know you want to start right away, but you need to read the whole chapter before you do. This chapter will help you install the security database and setting up your certificates. After reading that you can go ahead and download the projects I worked on. Here's the web application where the services are hosted and here's the client. Make sure you load them with VS in the same solution (these are for VS2005).

Keep in mind you'll have to change all the endpoint's url.

The web application also has a web service and an aspx page to test your credentials, so when you run the client you'll set up credentials you know are correct. If you can't log in using the aspx page, do not try to run the WCF client... it won't work.

Read Full Post

Tuesday, December 11, 2007

You know who's coming to town...

No, it's not Santa. I mean Santa is coming but he's not who I'm talking about... I'm talking about DeKlarit 4.5 for Visual Studio 2008. We are in our latest tests so expect it in a couple of weeks.

DeKlarit 4.5 has a few fixed bugs, many improvements our customers asked us for and a fully functional WCF addin letting you choose where to host your services and integrating the DeKlarit Security Database for authentication and authorization.

Here's a little preview :)

2008snapshot

Read Full Post

Saturday, October 27, 2007

Great customer support

I don't know if that's customer support. If you go back to one my latest post I was very pissed about not finding what I was looking for regarding Windows Communication Foundation (WCF) security features. I had read tons of articles, papers, blogs, and forum threads and I still couldn't figured it out (I still haven't). But then, after my post, somebody made a comment (you can go ahead and read it). This guy, Michael Green, told me he was from the WCF documentation team and he would be happy to hear what I had to say regarding the WCF documentation, so he left his email address for me to reach him.

So I did, I wrote an email telling him the articles I read and how incomplete those were. I told him that those articles were assuming some knowledge or tips of some kind that obviously I didn't know, that could be my fault but the articles did not mention them or have links to that info.

After a few days I got an answer, which I didn't expect. We discussed about one article in particular and exchange a couple of email. Bottom line: he appreciated my feedback, told me he was going to add the missing info in the docs and also told me to feel free to reach him anytime.

That's what I call great customer support... I'm still lost, I haven't achieved what I wanted to, but in some way, I feel good! :(

P.S: I checked and the article has not changed yet... I'll wait

Read Full Post

Wednesday, September 12, 2007

WCF timeout exception

I know I said WCF is not well documented, and I still believe it but I must say that when I posted about this issue I got a guy from Microsoft (named Richie) who really stuck with me to help me solve the problem. Even though I solved it, I still don't understand why. Now I'll post about it (and the solution) so hopefully I will help somebody with it and maybe some one can explain to me why the generated code with svcutil does not have what I want.

It all started implementing security features in the DeKlarit WCF Addin. When I finally got it working I started testing, and at some point the server did not respond and the client exited with a TimeoutException. I added trace log and message log and noticed that it always felt on the 15th communication.

After going thru the Indigo forum and Googled around I found the problem is due to the amount of concurrent connections opened and the timeout the have by default. What?!. Well, that's exactly what I thought, at least the NetTcpBinding (the one I'm using right now) has a default configuration that works in some limited cases.

How did I solve it? Closing the base channel every time so the connections won't add up. Even though it's been fixed I still don't understand why the generated code by the svcutil does not have those "close" calls.

Somebody? Anybody?

Read Full Post

Monday, September 10, 2007

WCF not well documented!

I know it's a pretty new technology, but I believe Microsoft should have done a better job with it's documentation. If you take a look at Microsoft's WCF forum you'll notice for the kind of questions people is doing that something is missing. There's of course the WCF for Dummies example where you host your services in your IIS and create a win app as the client. Everything goes smooth there. But what if you want your services to run as a Windows Service? what if you want to test it as a ConsoleApp?

I believe there's still some critical parts missing. If you come from Remoting you had an easy way to startup all your services with the RemotingConfiguration class, but in WCF there's nothing like it so if you have 10 or 20 services, as I have, you have the write two lines of code to create the service and open it to each and everyone of them... not cool! I've been trying to avoid it but I guess I'll have to run create that class myself. For some magical reason when you host your services in IIS, IIS starts them up. What about the rest of us?

It seems to me Microsoft was urged to release WCF and some critical parts are missing and other not well documented or not documented at all (it's real hard to find good info).

Read Full Post

Thursday, July 19, 2007

DeKlarit 4.3 will be released with WCF support!


That's what's been keeping me busy for the last couple of months, Windows Communication Foundation.
Originally started by Andres, the WCF addin is a great tool for quickly implement your own WCF architecture, or if your new to it, is a pretty good way get things working right away and see how it all works.

Basically you will have, as you did in the Remoting or the WSE addin, your own client where you change the data provider from SimpleDataAdapterFactory to WCFDataAdapterFactory and change the DataAdpaters reference in your module project for a reference to the WCFGenerator client dll where all the proxies are hosted. By doing that your app will no longer reach the DataAdapters in order to get to the data, instead it will try to reach some services. But where are these services? At the moment you have only one option, Internet Information Services, but you will have more options in the future. Also, right now only BasicHttpBinding and WSHttpBinding is supported.


So when you run the addin you'll get:
Service Client: the project you have to reference in your client application
Server web application: where the services are hosted
Service Implementation: that's the actual place were the services are implemented, so if you want to host your services in a console application or as a Windows Service the implementation is already done and it will be the same for all three kinds of project.
Service Contracts: that's where all the interfaces used as contracts are.


There's a pre-release version of DeKlarit 4.3 in the forum.


P.S: Windows Presentation Foundation will also be available in future DeKlarit versions, sorry, no 4.3

Read Full Post