Learning WCF - Visual Studio IDE - c#

I am new to WCF and I am using "Learning WCF: A Hands-on Guide" book for now. The book has used VS2008 for the examples, and I am not sure what Visual Studio IDE to use for the examples. I tried using VS Express for Web and it gives the following error:
"HelloIndigo.exe does not contain static Main method suitable at entry point'.
I can understand the cause of the issue, but I am not sure where to add the main method. So I used VS Express for Desktop and it worked fine, but as I kept going in the first chapter I could not proceed as there are no WCF service templates in the VS Express for Desktop version. VS2012 is available only in trial version for free, and it expires in 90 days. So what IDE should I should be using? If the answer is VS Express for Web, then how to fix the error for the example in first chapter?
The example provided in the book is
Host:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace Host
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService),new Uri("http://localhost:8000/HelloIndigo")))
{
host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new BasicHttpBinding(), "HelloIndigoService");
host.Open();
Console.WriteLine("Please <ENTER> to terminate the service host");
Console.ReadLine();
}
}
}
}
HelloIndigo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace HelloIndigo
{
public class HelloIndigoService : IHelloIndigoService
{
public string HelloIndigo()
{
return "Hello Indigo";
}
}
[ServiceContract(Namespace="http://www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}
}
Client:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace Client
{
class Program
{
static void Main(string[] args)
{
EndpointAddress ep = new EndpointAddress("http://localhost:8000/HelloIndigo/HelloIndigoService");
IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new BasicHttpBinding(), ep);
string s = proxy.HelloIndigo();
Console.WriteLine(s);
Console.WriteLine("Please <ENTER> to terminate client");
Console.ReadLine();
}
}
}
ServiceProxy.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace Client
{
class ServiceProxy
{
}
[ServiceContract(Namespace = "http://www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}
}

HelloIndigo should be compiled as a library (DLL) and not an executable. So there should be no Main method - it doesn't have one as a class library.
The point of the Host is that it will host the service library HelloIndigo and start listening for calls on an endpoint for that particular service.
Change HelloIndigo to compile as a class library and add a reference to HelloIndigo in Host. Then start up the Host process.

Related

Contract Namespace — No Conflict and Why?

I am learning WCF and as part of the learning, i found out that the namespace for the contracts should match. I wrote a contract class (both client and host have their own copy) and made their namespace to not match but my code still works. I have provided code for my contract and host class and how client is calling the contract. could someone please advise where i am wrong?
Client Contract Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace GeoLib.Client.Contracts
{
[ServiceContract]
public interface IMessageContract
{
[OperationContract (Name = "ShowMessage")]
void ShowMsg(string message);
}
}
Host Contract Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace GeoLib.WindowsHost.Contracts
{
[ServiceContract]
public interface IMessageContract
{
[OperationContract]
void ShowMessage(string message);
}
}
Calling Code in Client:
private void btnMakeCall_Click(object sender, RoutedEventArgs e)
{
ChannelFactory<IMessageContract> factory = new ChannelFactory<IMessageContract>("");
IMessageContract proxy = factory.CreateChannel();
proxy.ShowMsg(txtMessage.Text);
factory.Close();
}
Namespace in ServiceContracts or DataContracts are usually used for versioning as they allow for two objects with the same name to exist in different namespaces.
It seems, however, that you haven't defined a Namespace for your Service.
Defining a namespace would be like:
[ServiceContract (Namespace="http://yourcompany.com/MyService/V1")]
public interface IMessageContract
{
...
}
If you later introduce a new version of your Service with new implementation and put it in a separate namespace such as:
[ServiceContract (Namespace="http://yourcompany.com/MyService/V2")]
public interface IMessageContract
{
...
}
then you can keep the two services separated and have old clients calling version1 and new clients calling version2

Unable to discover NUnit test case

I'm trying use Nunit to test my simple program, but I dont know why it cannot discover my test cases... Compile result is pass
Here are my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace ConsoleApplication2
{
class FizzBuzz
{
public static string TestTarget(int parameters)
{
return parameters.ToString();
}
}
}
and
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace ConsoleApplication2
{
class FizzBuzzTest
{
[TestFixture]
public class fizzBuzzTest
{
[Test]
public void TestCase1()
{
Assert.That(FizzBuzz.TestTarget(1), Is.EqualTo("1222"));
}
}
}
}
If you are using Nunit 3.* you need to install NUnit3 Test Adapter.
For Nunit 2.* - NUnit Test Adapter.
In Visual Studio go to Tools -> Extensions and Update -> Online and find needed adapter.

Make Windows Service run a specific file

I've been watching some tuts on how to create a C# Windows Service; all good but no one says how to make te service run, at the end of installation, a specific file from installation folder(in my case hidden.vbs)(my app has 2 project: the service itself and the setup).
After the install of the setup, the service starts PROJECT_NAME.exe and PROJECT_NAME.svhost.exe
Tell me please if you need any other code in order to help me...
Here is my Program.cs
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
namespace PROJECT_NAME
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
new ServiceController(serviceInstaller1.ServiceName).Start();
}
}
}
Service1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace PROJECT_NAME
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
public void OnDebug()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
}
protected override void OnStop()
{
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
}
}
}
Also, here is a pic of my Solution Explorer http://i.imgur.com/wbqUGOc.png ; please tell me how or where should I import the files I need the service to run.
It's my first time in C#, I'm not willing to understand it now, but to make this service because I will need it in my work..
Your script can do one of several things to start a service:
Issue the net start console command to launch a service (e.g. net start "My Service Name")
OR
Programmaticaly, call the StartService API.

Namespace of IService not found

I'm trying to implement authorization into an existing WCF-service.
To do that I following a Microsoft Pattern & practices tutorial.
At Step 5, the service class should derive from IService, however my existing service class does not and when I add : IService to my class Visual Studio doesn't recognize it.
How can I derive from the interface as described in the article?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
namespace MyCompanyNamespace.API.IISServiceHost
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class CompanyNameAPIService : IService //not recognized!
{
public ApproveitAPIService()
{
}
}
}
From my point of view you have to Implement IService by yourself like in this Microsoft-Example.

ASP.NET Syntax and conventions

I am reading Designing Evolvable Web APIs with ASP.NET. In one of the exercises, the book has me edit a Controller using Visual Studio. This is being done in ASP.NET using C#. The template I used was the standard ASP.NET web application API.
I have edited the controller to the way the book shows (although it does not seem to give very specific directions). Here is what my controller looks like.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OAuth;
using WebApplication4.Models;
using WebApplication4.Providers;
using WebApplication4.Results;
namespace WebApplication4.Controllers
{
public class GreetingController : ApiController
{
public string GetGreeting() {
return "Hello World!";
}
}
public static List<Greeting> _greetings = new List<Greeting>();
public HttpResponseMessage PostGreeting(Greeting greeting)
{
_greetings.Add(greeting);
var greetingLocation = new Uri(this.Request.RequestUri, "greeting/" + greeting.Name);
var response = this.Request.CreateResponse(HttpStatusCodeResult.Created);
response.Headers.Location = greetingLocation;
return response;
}
}
I get errors on:
_greetings: A namespace cannot directly contain members such as fields or methods
PostGreeting: A namespace cannot directly contain members such as fields or methods,
_greetings : does not exist in the current context
Request : <invalid-global-code> does not contain a definition for 'request',
Created: HttpStatusCodeREsult does not contain a definition for 'Created'
As the error is trying to tell you, your fields and methods must be inside the class.
Check your braces.
Your _greetings field needs to be part of the class, as well as the PostGreeting method, it seems you just closed "}" of the class a bit early.
MOve the "}" before the _greetings field to the end of the file, like:
namespace WebApplication4.Controllers
{
public class GreetingController : ApiController
{
public string GetGreeting() {
return "Hello World!";
}
public static List<Greeting> _greetings = new List<Greeting>();
public HttpResponseMessage PostGreeting(Greeting greeting)
{
_greetings.Add(greeting);
var greetingLocation = new Uri(this.Request.RequestUri, "greeting/" + greeting.Name);
var response = this.Request.CreateResponse(HttpStatusCodeResult.Created);
response.Headers.Location = greetingLocation;
return response;
}
}
}

Categories

Resources