Hi I would like to use the same class library from my Silverlight application and WCF based service. I created a Silverlight C# class library and found the WCF service does not allow adding reference to Silverlight Project types. So
What should i do to make this work?
Can Silverlight invoke methods on Silverlight Class library if it communicates with WCF service?
Is silverlight always this hard?
Portable Class Library
http://msdn.microsoft.com/en-us/library/gg597391.aspx
Try making 2 class libraries, one for Silverlight and one for WCF. Both use the same C# source code files. In the second project, you can add the files as link (In Visual Studio in solution explorer: add existing item, and then in the drop-down Add button, choose "Add as link".
That's how we solved it for shared code. You have to limit yourself to library calls that exist in both worlds though.
Now it is easy :D - just use the .shared trick. It allows you to share the same code between server and client. Look here: http://msdn.microsoft.com/en-us/library/ee707371(v=vs.91).aspx
PS.: You can even add conditional directives on your .shared classes, like this:
#if SILVERLIGHT
MessageBox.Show("yay, I will run only on silverlight");
#endif
Related
When we create a Silverlight application, which includes two projects. The Silverlight project and the web project. I have a class in my SilverlightApplication1.Web namespace and I am trying to create an object of that class in my MainPage.xaml.cs class which is present in SilverlightApplication1 namespace.
But seems like I can't create an object of the web project from the Silverlight project. How can I do this? What's the concept?
When I create object I get error:
The type or namespace could not be found
It sounds like your Silverlight project doesn't have a reference to the web project. That's probably a good thing, mind you - I would suggest you set up a third project (as a Silverlight class library) called "Common" or something similar, and make both the web project and the Silverlight project refer to that. Put types which aren't web-specific or Silverlight-specific in that project, and then you can use them from both the web and Silverlight projects.
Am trying to create a WCF project by following the walkthrough here ... http://msdn.microsoft.com/en-us/library/bb386386.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-3, but got stuck at the first steps.
Bad - VSE Windows Desktop does not have the WCF service library templates.
Good - VSE Web Developer does, so I set up the WCF project in VSE Web Developer
Bad - Cannot open the WCF project in VSE Windows Desktop
Good - Find out that you can copy templates from VSE Web Developer Folders to VSE Windows Desktop, and I do
Good - VSE Windows Desktop now has WCF service library templates, so I try to create a new WCF project. VSE seems to comply, sets up the project folder ... but then
Bad - ... throws the error ...
The project file '... WcfService1.csproj' cannot be opened.
There is a missing subtype.
Subtype:'{blah blah}' us unsupported by this installation.
I even tried turning it off and turning it on again.
What next?
As I mentioned in my comment, a WCF Service Library is nothing more than a class library - the WCF Service Library project template just has additional stuff to make it quicker to set up the project. You can do the exact same thing by creating a class library.
Go to File -> New Project on the menu. In the window that opens up, expand Visual C#, then select Windows and select Class Library from the list in the center, give it a name and then click OK.
You'll have to add the Interface and the config settings for the service, but you should be able to copy and paste from the article you're following. Once that is done, voila, you have a WCF Service Library.
Edited for more details
The only thing the WCF Service Library template gives you is a boilerplate for a WCF Service Library - that includes the .cs file for the service implementation, the .cs file for the service contract (interface) That the service implements and an app.config file that has the necessary <system.serviceModel> entries.
Sticking with the article you linked to in your original post, here's how to do this without the template.
Step 1 and 2. Follow what I had above - create the class library. VS will create the project and you will see a file named class1.cs. This will be your service class. Rename it to WCFServiceLibrary1.cs if you desire.
Next add an interface and name it IWCFServiceLibrary1. This will be your service contract. You will need to update the WCFServiceLibrary1.cs file and add : IWCFServiceLibrary1 after the public class WCFServiceLibrary1, so it looks like this:
public class WCFServiceLibrary1 : IWCFServiceLibrary1
In the interface, add the [ServiceContract] attribute above the interface, like this:
[ServiceContract]
public interface IWCFServiceLibrary1
You will also want to add a reference to System.ServiceModel and using System.ServiceModel to your class and interface.
Step 3 and 4. Copy the code in the article to the proper files.
Step 5: You can test the service by hitting F5 and running the WCFTestClient.
You should then be able to follow the rest of the article.
It takes a little longer this way, but you will also gain a better understanding of what the WCF Service Library is.
Expectation management - I was expected to go through all the steps in the walkthough, but they might not be necessary (which is implied in the answers given). So, I tried a bare-bones set up, described here ...
Is it possible to start exploring WCF using Visual Studio Express for Windows Desktop 2013?
I've a C# solution that consists of an MVC project and a Windows Application project. Is there any way for MVC to call a Windows Application method and pass values to it?
I added Windows Application to References in the MVC project. But when adding the namespace reference using Windows Application name in the MVC class, it can't be found.
I appreciate any pointers.
Check that classes are public (else you can't call methods from the class) and that you have added reference to the project in your solution.
Take the code that's shared between the two, add a class library, and place it in there. That's what they are for. Past this, referencing a Windows application (.exe) is sure to be an issue even further down the road.
hopefully this is a quick one. If I have c# class files on WebsiteA.com - can I reference them in WebsiteB.com? Both sites are on the same server, so I was hoping to reference them on a static address? (d:/inetpub/wwwroot/websiteA-com/app_code/MyClass.cs)
Is this possible? Or - do I have to copy the class file to WebsiteB.com and simply use that?
I just want to avoid repetition when I need to make any changes - avoiding changing both sites.
I've built both sites in .net 4 using visual studio 2010 express.
Thanks
If I were you, I would take all the classes needed by both websites and create a Class Library Project (DLL). Then reference that project in each website and use the classes. This means each site will be deployed with the same DLL but you will have a single project for common classes.
Hope this helps (I know it's not strictly the answer)
Sure you can.
I don't think that adding the class from the app pool of the IIS folder is a good idea though.
I believe it's a better practice though to make a class library as a separate project called for example Shared and reference it in both projects.
in Visual Studio you can "Add existing" then choose the "Link" option to add source code from another project
this can be much easier to manage than a shared class library - you can even include the same code in wildly different projects (compact framework, different versions of dot net, etc.)
Have written all the code in a silverlight class library (dll) and linked this same library to my web app and silverlight app, is there a way to avoid the "Compiler Error Message: CS0433" or do I have to create a separate dll for the web app?
Error mostly occurs when XElement is called...
You need to create two projects, but can add the same CS file using Add Existing Item as a Link. http://msdn.microsoft.com/en-us/library/9f4t9t92.aspx explains it.
http://www.scip.be/index.php?Page=ArticlesNET28 is a nice read with related info.
Do you mean you are referencing a single dll from both the web app and the Silverlight app? I would have two versions of the dll (and two project files); one built for regular .NET (for use in the web app), and one for Silverlight; the main difference being the target framework and the references.
If you don't want to deal with having to maintain two project files (when you add classes etc), then you can use this trick to reduce this overhead.