In this post, I will show you how to consume the custom WCF using asp.net Ajax. ASP.NET Ajax cannot consume services that are exposed over basicHttpBinding. However, it can consume services that are exposed over webHttpBinding and use the enableWebScript behavior. The steps are shown below:
1. Modify the web.config to add a new endpoint using webHttpBinding and enableWebScript behavior.
4. Now navigate to http:// ybest /SitePages/JSTestPage.aspx and you will see the results as below.
You can download the source code here.
In the first 2 parts of this series, I showed you how to deploy custom WCF deploy into SharePoint and consume such WCF in a console application. In this post I would like to show you how to consume the same WCF service in a Silverlight application. You can download the source code here
1. Create Silverlight application and add the WCF references as below. (I know I told you it is not a good practice to consume WCF in this way when you have control over both WCF server side and client side and both server and client side are written using .net, it is a little bit harder to consume the WCF services in Silverlight in this way as you cannot add non-Silverlight class assembly to the Silverlight project.).
2. Add 2 labels controls and one button control to the form and add the following code to the button click event.
3. Deploy the Silverlight into SharePoint by creating SharePoint module item template and add the Silverlight to a sitepage. For details of the configuration, check my previous blog post here.
In the last post of this series, I showed you how to create and deploy custom WCF to SharePoint. In this post, I will show you how to consume the custom WCF service in a console application the right way. You can download the complete source code here.
The easiest way to consume a WCF is to add a service reference using visual studio and following the wizard visual studio will generate the necessary proxy for you. You can check my previous blog post here for details of this approach.
However, I will consider this approach as the bad way of consuming a Custom WCF services assuming both the services and client are using .net and WCF. The reasons are as below:
- Every time you add service reference, visual studio generates the necessary service interface and data contracts in your application. So you have multiple copies of the same files in different places.
- Because you have multiple copies of the same files in different places, every time you update your services you need to update your services references and redeploy your solution.
- After you add the service references, visual studio add app.config files in your application and in your app.config visual studio add unnecessary amount of default attributes in the system.serviceModel section.
After you understand why it is bad to just add service reference to consume WCF services, you might ask what is the right way of consuming the WCF services? If you have used the new SharePoint client object model, you know that the client object model talk to SharePoint using WCF services, the way you use the client object model for console app is to add the Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime dlls to the application and you are all set to communicate with WCF services from SharePoint. So how can we consume the WCF services in the same as the Client object model? You need to do the following:
- You need to separate your WCF services in 3 separate projects (dlls) as specified in the last post, all service contract in a project, all service implementations in a project, all data contract in another project.
- Create forth project called BestPracticeClient for WCF client, in this project you need to references your Service contract project and data contract project and create WCF client using ChannelFactory.
- Create a console app references the BestPracticeClient , Service contract and data contract project dlls and you can consume the WCF services the same way as you do for SharePoint client object model.
You can download the complete source code here.
In SharePoint2010, it has lots of asp.net web services as well as some new WCF services to be used in your client application. The new client object model in SharePoint 2010 uses WCF as well. However, most of the built-in web services are generic and does not use your own business object. If you would like to have object that matches your own business scenario, you need to create your own WCF services. Today, I will show you how to create and deploy your custom WCF. You can download the source code here.
1. Create a new empty SharePoint project and deploy it as a farm solution.
2. Add 3 WCF Service Library projects into the solution as shown below. The reasons for this project setup are explained in a video screencast here.
3. In the YBBEST.WCFServiceImplementation project, add these YBBEST.WCFServiceContract and YBBEST.WCFDataContract project references.
4. In the YBBEST. WCFServiceContract project, add YBBEST.WCFDataContract project references.
5. Add the classes needed for the WCF project as below.
6. Add ISAPI SharePoint Mapped Folder to the YBBEST.CustomWCF SharePoint project, add YBBEST folder and in the folder add web.config file, add CustomService.svc file.
8. Add the following to your web.config
9. Add the key from the YBBEST.CustomWCF project to the 3 WCF projects as a Link and sign the project using this key. For detailed explanation, please check MSDN article.
10. Add the project output dlls to the SharePoint Package for deployment into SharePoint.
a. Go to Package of the YBBEST.CustomWCF SharePoint project
b. Add Assembly from Project Output as below.
11. Deploy the project to your SharePoint server and navigate to the service url http://ybbest/_vti_bin/ybbest/CustomService.svc , you will find the following message, since I do not enable Metadata publishing for this service.
12. You need to change your web.config as follow to enable Metadata publishing for this service.
You can download the source code here.
When working with SharePoint2010 web services, the first thing you might do is to create a console application to test your idea. Now, I will show you how to create one to consume the SharePoint Lists Web Service. There are two ways of adding a service references, one is to add a service reference, and another way is to add a web reference. By adding a service reference, this will generate code based on latest .NET Framework Web Services technology (WCF) and by adding a web reference instead of a service reference. This will generate code based on .NET Framework 2.0 Web Services technology.If you like to call the same web service from Office 365 ,you can check my blog here.
To add a service reference, you need to do the following:
Open visual studio to create a console application then add service references. In the address textbox, type the SharePoint Lists Web Service URL in the format of http://<site>/_vti_bin/Lists.asmx
In the console app, write the following code to query the all the lists. You need to set the web services credentials as highlighted below.
The final step is to make application changes in the app.cofing file.
You need to change from
- Press F5 to run the application, it will produce the following result.
You can download the complete application from here.
Could not find default endpoint element that references contract ‘Your Service’ in the ServiceModel client configuration section.
When I consume the WCF in a class library project by adding the service reference , I got the following exception message :
Your search - Could not find default endpoint element that references contract 'yourService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. - did not match any documents.
In order to solve this , you will need copy the System.ServiceModel section of the generated app.config file to the executing assembly project’s .config file (web.config for web application project, app.config for windows or console project) . Then it run like a charm. This is because the app.config file in the class library project is not used by the WCF ,instead the executing assembly project’s .config file will be used.
The page you are requesting cannot be served because of the extension configuration
I was setting up a project on my local box that was hosting a WCF service. These web services have a .svc file extension and apparently IIS 7 out of the box doesn’t know what to do with them.
The error you get will look something like this:
HTTP Error 404.3 – Not Found
The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map. Detailed Error InformationModule StaticFileModule
And there will be more info listed below that shows some items related to your local machine. So after much trial and error and a lot of Google slapping, I finally found the answer to my problem.
1. Open Visual Studio 2008 Command prompt.
2. Navigate to C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation
3. Run this command: servicemodelreg –i
Apparently when IIS 7 is installed they don’t turn everything on for you and so you are required to do it yourself.
Hopefully, this has saved some of you some time and frustration.
The original post is http://tonytriguero.com/iis-7-and-webservices-svc-file-extension/,I will post here for my own references.
Some other references: