Sunday, September 25, 2011

Itrarting Bewteen Date Range using Foreach C#.

There are some situation when we want to itrate trough a range of date.
Hear is ample how you can itrates through the date range using foreach in C#

for itrating you will need to first create a function which will convert the date range in inumaration formate so that you can itrates.

For this i have created the follwing function.

public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1)) yield return day;

Now you can use the code like this.

foreach (DateTime day in EachDay(strartDate, EndDate)
 //Do something


Tuesday, September 20, 2011

Hosting WCF Service in IIS

Create a WCF service

1. Create a blank solution and add a WCF Service Library project to it. For the purpose of this post, I am going to create a simple GreetingService that has a method Greet. This method accepts a single parameter name and returns a string “Hello “ appended to name

2. The following is the code for the GreetingService WCF service. We define a service contract, mark the operations in that contract that are going to be exposed to the external world and then implement the service.

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace Wcf.Samples.ServiceLibrary
    public interface IGreeting
        string Greet(string name);


using System;
using System.Collections.Generic;
using System.Text;

namespace Wcf.Samples.ServiceLibrary
   public class GreetingService : IGreeting
      #region IGreeting Members
      public string Greet(string name)
         return "Hello - " + name;

3. Now that you added the service contract and its implementation (the GreetingService), build the project.

Host the service in IIS

1. Now we create a Web site that will host the WCF service that we created in the previous section. For this, right-click the solution in the solution explorer and from the context menu, select Add -> New Web site ->WCF Service as shown in the following figure.

2. This will create a WCF Service Web site into your solution along with the standard folders that get created for a Web site project (App_Code, App_Data etc). A new type of file called Service.svc will be generated and placed into the root of the Web site project, along with the corresponding code-behind file (Service.cs). The Service.cs is provided to implement a WCF service, which then can be referred in the Service.svc file. Since we already have created the WCF service in a separate assembly, we will refer to it in the Service.svc. As such we don’t need Service.cs and hence, it can be deleted.

3. Open Service.svc file and modify the single line in it like this:

4. In the above statement we point to the fully-qualified class name that implements the service that we want to host in IIS.

5. Add the reference of the Wcf.Samples.ServiceLibrary project to the web-site project and build the web-site.

6. This completes the creation of WCF service. Now, in order to let the service communicate with the external world, we need to define the communication behavior of the service. For defining this behavior, we will use the Service Configuration tool that comes along with Visual Studio 2005.

7. From the main menu of Visual Studio 2005, select Tools -> WCF Service Configuration Editor. The WCF Configuration Editor window will open up. Select File -> Open -> Config File… from the main menu. Browse and select the Web.config file of the WCF web-site.

8. Once selected, the screen will be displayed as follows. The Web.config already has the configuration for the default service MyService under the Services node in the left-panel. Select this service and deleted it. We are going to create a new configuration for our service.

9. Right-click the Services node and select “New service” from the context menu. A service with service type NewServiceType will be created. On the right-panel, select the property “Name” and click the ellipsis. Service Type Browser will open up. Browse the bin folder of the web-site and locate the service assembly Wcf.Samples.ServiceLibrary and double-click it. The Service Type Browser dialog now will list the service “Wcf.Samples.ServiceLibrary.GreetingService” service. Select this service and click Open. This will set the service-type that we are going to configure.

10. Now for the above service, we first need to specify the end-point. Right-click Endpoints node in the left-panel of the WCF Service Configuration Editor and select “New Endpoint”. This will create a default end-point with its properties being displayed in the right-panel.

11. Set the following properties:



· Here 28053 is the port-number where the local web-server is running. You need to check your port number and enter it here appropriately.



· You can click the ellipses to open the Contract Type Browser and select the appropriate assembly and contract from the bin directory of the web-site.

12. This sets the basic communication for our service. In order to enable the service for metadata exchange (thereby allowing us the browse its wsdl), we need to set the metadata exchange properties for the service. To do this, expand the Advanced node in the left-panel, right-click Service Behaviors and select “New Service Behavior Configuration”. This will add a new Behavior Configuration by the default name NewBehavior. Behavior is a collection of attributes (here, service attributes) that can be set and applied to the service together. Right now, we are going to define a behavior that allows the metadata exchange on the service. Set the Name property of the new behavior configuration to mexBehavior.

13. In the “Behavior element extension position” (lower part of the right-panel), click “Add” button. This opens up a dialog “Adding Behavior Element Extension Sections”. Select serviceMetadata from the list and click “Add” on the dialog. This adds the extension serviceMetadata to the grid in the right-panel. Double-click the extension to open up its property-page. Set the property HttpGetEnabled to true.

14. Now that we have defined the behavior separately, we need to associate the behavior with our service. To do this, select the Wcf.Samples.ServiceLibrary.GreetingService under the Services node in the left panel. The right-panel will display its properties. Select the BehaviorConfiguration property, and select “mexBehavior” from the drop-down.

15. This sets the service configuration and allows the service to communicate with the external world. Save the configuration by selecting the menu File->Save. Close the WCF Service Configuration Editor.

16. Test that the service is hosted by running the WCF Web Site application. The following screen should be displayed.

Related Posts Plugin for WordPress, Blogger...