No set method for property ‘Message’ in type ‘MyProject.MyType.
A DataContract can’t have a property with no set method (http://msdn.microsoft.com/en-us/library/ms733127.aspx). However, you can use a private set accessor if allowing modification of the property is prohibited. :
[DataMember]
publicobject Message
{
get { return _message; }
private set { _message = value; }
}
HTTP could not register URL http://+:80/MyService/ because TCP port 80 is being used by another application.
Happen if IIS is running (very likely!). Change the port to another one. Example :
The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.
You have an error, but you don’t get all the details. To get them :
On a previous post called Reality of type sharing in WCF. I talked about a way to use existing types for a WCF service instead of the generated proxies. Previously, we were unable to do that in Visual Studio 2005, but it’s doable Visual Studio 2008.
Start by adding the library you would use for type sharing on the project you will add a service reference.
When you add a service reference, click on the Advanced… button.
Then, make sure the Reuse types in referenced assemblies checkbox is checked.
You can fine tune further, if you do not want to use some assemblies for type sharing.
When you want to create a new WCF project, you are presented with 4 different templates to use :
Sequential Workflow Service Library
Syndication Service Library
State Machine Workflow Service Library
WCF Service Library
Each one has it’s own particularities. It’s important to know which one to choose but you should know that the template only generate things you need to get started. You can always start from an empty WCF Service Library and build on top of it. Anyway, here is an explanation for each one.
Sequential Workflow Service Library
You basically create a sequential workflow project that made operations available by WCF. I have no experience with Workflow Foundation so I will not go further into details.
Syndication Service Library
This template is great. It present you a way to expose an RSS or Atom feed via a WCF Service contract. This is built-in stuff already present in the framework.
To expose a feed, you need an operation that return a SyndicationFeedFormatter (in the System.ServiceModel.Syndication namespace). Then create a SyndicationFeed and add SyndicationItem to it. Finally, create a Atom10FeedFormatter or Rss20FeedFormatter and return it.
SyndicationFeed feed = new SyndicationFeed("Feed Title", "A WCF Syndication Feed", null);
List<SyndicationItem> items = new List<SyndicationItem>();
// Create a new Syndication Item.
SyndicationItem item = new SyndicationItem("An item", "Item content", null);
items.Add(item);
feed.Items = items;
// Return ATOM or RSS based on query string// rss -> http://localhost:8731/Design_Time_Addresses/SyndicationServiceLibrary1/Feed1/// atom -> http://localhost:8731/Design_Time_Addresses/SyndicationServiceLibrary1/Feed1/?format=atomstring query = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["format"];
SyndicationFeedFormatter formatter = null;
if (query == "atom")
{
formatter = new Atom10FeedFormatter(feed);
}
else
{
formatter = new Rss20FeedFormatter(feed);
}
return formatter;
State Machine Workflow Service Library
Another template that expose a Windows Workflow as a Service. This time the type of Workflow is a State Machine. A state machine workflow contrary to a sequential workflow is driven by external events. Transition between states are based on certain events until it reach the final state.
WCF Service Library
The most basic project to create a WCF service. You can use it to do everything you want with the most control.
Suppose you have the interface of a repository, let’s say IEntryRepository. Then you have the actual SqlEntryRepository and TestEntryRepository. When I am doing TDD, I am testing the TestEntryRepository. But it will not be used in production. Why bothering with testing it then?
Because TDD is a design process. Even if I am testing my code, I am also designing it. The SqlEntryRepository will benefits from the actual design of the repository we use for testing.
I have just finished listening to the first episode of a new podcast. It’s a discussion about self-improvement for developer with Chad Myers, David Laribee and Jeremy D. Miller. It’s very inspiring and I recommend that you listen to it if you are serious about software development.
05/12 Source code release of the ASP.NET AJAX Script Profiler helper control: To support the ASP.NET AJAX script combining feature which shipped in the .NET Framework 3.5 SP1 Beta release, we’ve added the ASP.NET AJAX Script Profiler helper control source and binaries to this project. This control helps you to identify the scriptreferences used in your ASP.NET AJAX page to use with ASP.NET AJAX script combining. You can find this release here: ScriptReferenceProfiler Source and Binary Release. In addition this control is used in a Screencast for ASP.NET AJAX script combining on the ASP.NET site.
05/12 Release of the ADO Data Service AJAX Client : The ADO Data Service AJAX Client Library enables you to consume an ADO.NET Data Service from client script in an ASP.NET AJAX Web page.
A reader asked mehow we can display the solution node in Visual Studio when you have only one project. This feature is turned off by default. What you need to do is to go in your Tools/Options menu and click on Projects and Solutions. Then you need to check the Always Show Solution checkbox.
Like promised, on a previous post, I will show you how to mock HttpRequest. I will use Rhino Mock but you can use the one you want (moq, typemock, etc).
On you controller base class, a property called Request is exposed. Behind the scene you are accessing an HttpContextBase instance (this class is contained in the System.Web.Abstractions library), which is providing you the HttpRequest you want to mock.
So, let’s start by creating a mock of these two object.
var mocks = new MockRepository();
var mockedhttpContext = mocks.DynamicMock<HttpContextBase>();
var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>();
The mockedHttpRequest will be provided by the mockedHttpContext. So we will setup the mocked HttpContextBase.Request property to return the mocked HttpRequestBase.
But, what would we do with a mocked HttpRequestBase? When you need to test an action on a controller that will retrieve values for the Request.Form property you could create a NameValueCollection and tell the Form property of the HttpRequestBase instance to return it.
NameValueCollection formParameters = new NameValueCollection();
formParameters.Add("txtUsername", "username");
formParameters.Add("txtPassword", "password1");
SetupResult.For(mockedHttpRequest.Form).Return(formParameters);
I really like playing with the ASP.NET MVC framework!!!
100th post. I want to thanks everyone who came visiting me regularly. Over the last year, the amount of visits grew each weeks. I remember when I got the 100 visits mark. Now I am at an average of 120 visits per day with a day of 199 visits. As you can see below, it’s pretty clear where the weekend is.
Here is a gift for you all !!! (I think he had a lot of spare time …) Enjoy!
I just got a hard time figuring how I could be mocking the HttpContext so that I would be able to mock HttpRequest. But once you understand the principle, it is fairly easy.
First thing we need to know is that the HttpContext is held inside a ControllerContext object. Once we have instantiated one, we can then put it inside our controller.
controller.ControllerContext = new ControllerContext(mockedhttpContext, new RouteData(), controller);
Where mockedHttpContext is your mocked HttpContextBase object.
Inside the HttpContextBase object reside a lot of useful stuff that can replace with our mocks. By example, the HttpRequest, HttpServer, HttpApplication, etc, are all contained inside. So, getting familiar with this basic knowledge is a must to do testing usefully.
Next post : Mocking HttpRequest.Form to return what we want.
I haven’t really done any testing in ASP.NET MVC until now. I started the development of a more serious application and decided to do it TDD. My first test was to make sure the Index action render the Index view. So as an informed developer, I started reading some blog posts to discover that in order to test a controller you need to mock a lot of things. I followed the screencast of Scott Hanselman and implemented his MvcMockHelper. It didn’t worked. The ViewContext on the view engine remained null, resulting in an NullReferenceException. I downloaded the source of the last build to see for myself what was wrong. Surely I don’t understand, because nowhere in the source they are setting the ViewContext on the ViewEngine. They just fill a RenderViewResult object and return it back.
This is how I figured out how we can now test controllers. With the new Interim build of the 04/16, we don’t have to mock the HttpContext, HttpRequest and so on anymore. No need to do a fake view engine and no more mocking needed to test the RenderView method.
[Test]
publicvoid Index_Should_Set_The_Index_View()
{
EntryController controller = new EntryController();
var result = (RenderViewResult)controller.Index();
Assert.That(result.ViewName, Is.EqualTo("Index"));
}
So if you have any other operations (RedirectToAction and Redirect for example), it will return an object of type ActionResult that you can cast to the appropriate inheritor (RenderViewResult, ActionRedirectResult, HttpRedirectResult) to get a lot of information that you would have to retrieve manually with a mock.
I really like the simplicity of it and can’t wait to see more improvement. This is an immense opportunity to learn a great deal of different things.
This is intended to be a quick start for anyone who want to start playing with the latest version of ASP.NET MVC without going through the origin and goals of the MVC pattern. I will use the ASP.NET 0416 MVC Interim Source Code Release for this. Keep in mind, that this will probably all change in a near future.
First step, download and build the source. You will need to have the Moq library (more about this mocking framework in a future post).