Answered by:
How to programmatically set endpoint of WCF service in web.config file ?

Question
-
Hello,
I have cretaed and hosted WCF service in sharepoint 2010 web application through VIsual studio.
for this purpose I need to manually add endpoint configuration of WCF service in sharepoint web.config file, then only i will be able to use this service.
Can you please let me know the approaches to automatically add endpoint to web.config of sharepoint web application when I deploy WCF service to sharepoint environment ?
Very keen to see the answers !!
Thanks !!!
Dipti Chhatrapati
Tuesday, April 10, 2012 10:02 AM
Answers
-
Hi,
The code given here for "Consuming the WCF Service" in your client application.
For confirming the just created WCF service,copy your wsp file and deploy it on another SP environment and type the URL[http://siteurl/_vti_bin/MyService/MyService.svc/MEX],If you get the XML entries on your browser then its dynamically built-in and works fine.
Murugesa Pandian.,MCTS|App.Devleopment|Configure
- Marked as answer by Dipti Chhatrapati Tuesday, April 17, 2012 4:36 AM
Monday, April 16, 2012 12:44 PM
All replies
-
That's not necessary. Use the SharePoint WCF factories.
http://msdn.microsoft.com/en-us/library/ff521581.aspx
Scot
Author, Professional Business Connectivity Services
Author, Inside SharePoint 2010 Blog, www.shillier.com
Twitter, @ScotHillier
SharePoint Trainer, Critical Path Training- Proposed as answer by Nazre1 Tuesday, April 10, 2012 10:56 AM
Tuesday, April 10, 2012 10:38 AM -
Change it programmatically via a SharePoint feature: http://weblogs.asp.net/wesleybakker/archive/2009/01/21/web.config-modifications-with-a-sharepoint-feature.aspx As opposed to Scot, whose advice I deeply appreciate, I do see reasons to add explicit endpoint configurations. You may also want to check out this link: Back to the future (this article is not the answer, but it's related to the topic and you may be interested in reading it).
Kind regards,
Margriet BruggemanLois & Clark IT Services
web site: http://www.loisandclark.eu
blog: http://www.sharepointdragons.com
- Edited by Margriet Bruggeman Tuesday, April 10, 2012 1:04 PM
Tuesday, April 10, 2012 1:01 PM -
Thanks to all of you for the reply !!
well I am trying for "SharePoint WCF factories." approach , however I am getting following error while deploying the service on sharepoint on service url
however I am getting ENdpoint at following URL
http://siteurl/_vti_bin/MyService/MyService.svc/mex
I have set servicehost as follow in service file
<% @ServiceHost Service="MyServiceProject.Service1,MyServiceProject,Version=1.0.0.0, Culture=neutral, PublicKeyToken=04729017a69cac3a" Debug="true" Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressBasicHttpBindingServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Language="C#" CodeBehind="Service1.cs" %>
can you please let me know where I am wrong !!!! :(
Dipti Chhatrapati
Tuesday, April 10, 2012 3:03 PM -
No closing %> tag
Scot
Author, Professional Business Connectivity Services
Author, Inside SharePoint 2010 Blog, www.shillier.com
Twitter, @ScotHillier
SharePoint Trainer, Critical Path TrainingTuesday, April 10, 2012 3:36 PM -
Thanks Scott for the reply.If I am not wrong I have specified "%>" tag at end of servicehost as per specified above like
CodeBehind="Service1.cs" %>
Please let me know where I am wrong ? or where I misunderstand your reply !!!
Thanks a lott again !!!
Dipti Chhatrapati
Wednesday, April 11, 2012 4:29 AM -
The SVC file should look like this:
<%@ ServiceHost Language="C#" Service="SLThemeService.ThemeService, SLThemeService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9fb96e74f4698482" Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Scot
Author, Professional Business Connectivity Services
Author, Inside SharePoint 2010 Blog, www.shillier.com
Twitter, @ScotHillier
SharePoint Trainer, Critical Path TrainingWednesday, April 11, 2012 5:38 AM -
Hi Scott,
I have followed your reply and make changes in my project as below
<%@ ServiceHost Language="C#" Service="MyServiceProject.Service1,MyServiceProject,Version=1.0.0.0, Culture=neutral, PublicKeyToken=04729017a69cac3a" Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
after deploying this service I get following result in browser
I am also getting same result for following url
http://siteurl/_vti_bin/MyService/MyService.svc/mex
Is that right ? What further chnages I need to follow ? Can you please guide me to make this service available ?
Thank you for your time !!
Dipti Chhatrapati
- Edited by Dipti Chhatrapati Wednesday, April 11, 2012 5:50 AM
Wednesday, April 11, 2012 5:49 AM -
Are you using the REST WCF ? If you using uritemplate of WebGet or WebInvoke decoration attribute then use /attributeName after your .svc
And make sure you deleted the <serviceModel>tag on your WCF application's config file.
Murugesa Pandian.,MCTS|App.Devleopment|Configure
- Edited by Murugesa Pandian Wednesday, April 11, 2012 6:48 AM
Wednesday, April 11, 2012 6:47 AM -
As I understand you need to put '/mex' at the end of the url to get the mex endpoints. You can check more details at: http://ranaictiu-technicalblog.blogspot.com/2011/03/sharepoint-2010-create-custom-wcf.html
Thanks,
Sohel Rana
http://ranaictiu-technicalblog.blogspot.comWednesday, April 11, 2012 7:16 AM -
Dipti,
Rather than go back and forth with questions, try this...
Here is an excellent article that walks you through the process of creating a simple service using the factory approach:
http://blog.sharepointbits.com/2010/04/custom-wcf-services-in-sharepoint-2010_17.html
The only thing missing from this walkthrough is the configuration you must do to get Visual Studio to properly handle token replacement for SVC files. Here is a post explaining how to accomplish this:
Follow the first post completely. When you are done, use the second post just before you compile.
Scot
Author, Professional Business Connectivity Services
Author, Inside SharePoint 2010 Blog, www.shillier.com
Twitter, @ScotHillier
SharePoint Trainer, Critical Path Training- Edited by Scot Hillier Wednesday, April 11, 2012 10:32 AM
Wednesday, April 11, 2012 10:32 AM -
Factory type is BasicHttpBinding,So you must include the MEX after your http://siteurl/_vti_bin/MyService/MyService.svc/ URL.
Right Click on the Project ->Un Load the Project.After edit the .proj file
and place the <TokenReplacement> svc</TokenReplacement> tag under <PropertyGroup> by this way and Reload this project and deploy them.
You do not want to use the static end point entries in the site config file in sharepoint.It will build the endpoint dynamically.Can be host this WCF any Site or deploy them in any of the server which is part of your farm.
Otherwise you'll get "Bad Request" error or above error.
Murugesa Pandian.,MCTS|App.Devleopment|Configure
- Edited by Murugesa Pandian Thursday, April 12, 2012 12:03 PM
Thursday, April 12, 2012 11:56 AM -
Well , I tried to access endpoint without adding service reference using following code lines
//Access WCF service Centre Detail Endpoint WSHttpBinding binding = new WSHttpBinding(); EndpointAddress address = new EndpointAddress(SPContext.Current.Web.Url + "/_vti_bin/MyService/MyService.svc"); ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, address); IGetCentreNames client = factory.CreateChannel();
And I am able to access service dynamically only and I dont need to add entried in web.config !!!
can you please let me know whether this one is valid approach ?
Thanks,
Dipti Chhatrapati
- Edited by Dipti Chhatrapati Monday, April 16, 2012 12:35 PM
Monday, April 16, 2012 12:31 PM -
Absolutely. In fact, I feel this is a very elegant approach.
Kind regards,
Margriet BruggemanLois & Clark IT Services
web site: http://www.loisandclark.eu
blog: http://www.sharepointdragons.com
Monday, April 16, 2012 12:37 PM -
Hi,
The code given here for "Consuming the WCF Service" in your client application.
For confirming the just created WCF service,copy your wsp file and deploy it on another SP environment and type the URL[http://siteurl/_vti_bin/MyService/MyService.svc/MEX],If you get the XML entries on your browser then its dynamically built-in and works fine.
Murugesa Pandian.,MCTS|App.Devleopment|Configure
- Marked as answer by Dipti Chhatrapati Tuesday, April 17, 2012 4:36 AM
Monday, April 16, 2012 12:44 PM -
Hi Murugesa,
I deployed service project's wsp to another application and I dynamically getting that service to another server and its working fine.
should I go ahead with this approach ? If No please let me know the reason !!!
Thanks a ton !!
Dipti Chhatrapati
Monday, April 16, 2012 1:30 PM -
yes, this is working solution. Moreover, when you need to call WCF service without Sharepoint context (e.g. from Sharepoint timer job) it is preferable way (although in case of Sharepoint job it is also possible to add WCF configuration declaratively via owstimer.exe.config file, but it is not relevant here). The disadvantage of this approach is that if you will need to change configuration (e.g. change security settings of the endpoint or increase message quota) you will need to recompile your project and redeploy dll into the GAC. If it is not problem for you, you can use this approach.
Blog - http://sadomovalex.blogspot.com
CAML via C# - http://camlex.codeplex.com- Marked as answer by Dipti Chhatrapati Tuesday, April 17, 2012 4:37 AM
- Unmarked as answer by Dipti Chhatrapati Monday, April 23, 2012 4:56 AM
Monday, April 16, 2012 1:45 PM -
Yes.This will be fine.! go ahead with this approach.
Further you can make your WCF service be more reliable and flexible,you can use and follow the SPG [CodePlex Project],Exception handling using Ent Libr 5.0.
You can download some code samples from Code Samples for SharePoint 2010 for reference and learning further.
Murugesa Pandian.,MCTS|App.Devleopment|Configure
Monday, April 16, 2012 3:57 PM -
Thanks a lott Murugesa and sadomovalex for your feedback !!! :)
Dipti Chhatrapati
Tuesday, April 17, 2012 4:36 AM -
Hey Guys !!!
Again I am getting following issue with above mentioned approach !! :(
I have created WCF service and hosted to sharepoint environment.which is working fine.
Now at client project I am consuming that service through programmatically without adding service reference as below
//Access WCF service Endpoint private static WSHttpBinding binding = new WSHttpBinding(); private static EndpointAddress address = new EndpointAddress(SPContext.Current.Web.Url + "/_vti_bin/MyService/GetSErvice1.svc"); private static ChannelFactory<IGetService1> factory = new ChannelFactory<IGetService1>(binding, address); private IGetProgramDetail client = factory.CreateChannel();
However I am getting following error:
The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'NTLM'.
Note: As I am accessing WCF programmatically I do not need to change anything in web.config for endpoint as well no need to add service reference.
Can you please let me know how can I resolve this error ???
Dipti Chhatrapati
Friday, April 20, 2012 8:00 AM -
Hi Dipti,
I think you should create new thread, Since your First question is already marked as Answer.
However, Please try with this sample code-change WCFIdentityPoolUserID and URL.
WSHttpBinding binding = new WSHttpBinding(); binding.MaxReceivedMessageSize = 2147483647; binding.ReaderQuotas.MaxDepth = 64; binding.ReaderQuotas.MaxStringContentLength = 2147483647; binding.ReaderQuotas.MaxArrayLength = 2147483647; binding.ReaderQuotas.MaxBytesPerRead = 4096; binding.ReaderQuotas.MaxNameTableCharCount = 2147483647; EndpointIdentity ei = EndpointIdentity.CreateUpnIdentity("<WCFIdentityPoolUserID>"); EndpointAddress endpoint = new EndpointAddress(new Uri("http://appsites/_vti_bin/myWCF.Service/MYWCFService.svc"),ei); MYWCFServiceClient proxy = new MYWCFServiceClient(binding, endpoint);
Hope this Helps:)
Regards,
Sushil
Friday, April 20, 2012 9:08 AM -
The problem is that you need to configure the client for NTLM
EndpointAddress endpoint = new EndpointAddress(deliveryReader.GetString(0));
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;MiniCRMNotifications.NotificationEndpointClient client =
new MiniCRMNotifications.NotificationEndpointClient(binding,endpoint);
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ChannelFactory.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;Scot
Author, Professional Business Connectivity Services
Author, Inside SharePoint 2010 Blog, www.shillier.com
Twitter, @ScotHillier
SharePoint Trainer, Critical Path TrainingFriday, April 20, 2012 9:58 AM -
Hi Scott ,
As I have not added service reference in project from where I am consuming WCF service !!!
Due to above step I am unable to get client object also !!!
would you please let me know how can I set below code ?
MiniCRMNotifications.NotificationEndpointClient client =
new MiniCRMNotifications.NotificationEndpointClient(binding,endpoint);
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ChannelFactory.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;however I have Changed my code with following lines
//Consume WCF service Endpoint WSHttpBinding binding = new WSHttpBinding(); binding.Security.Mode = System.ServiceModel.SecurityMode.TransportWithMessageCredential; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm; EndpointAddress address = new EndpointAddress(SPContext.Current.Web.Url + "/_vti_bin/MyService/GetSErvice1.svc"); ChannelFactory<IGetService1> factory = new ChannelFactory<IGetService1>(binding, address); factory.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation; IGetProgramDetail client = factory.CreateChannel();
Whether above code is correct ? If not , would you please let me know what I have missed and How can I resolve that ?Thanks for your time !!
Dipti Chhatrapati
Monday, April 23, 2012 5:06 AM -
Thanks Sushil,
I am getting following error while I tried your sample code , also I dont want to mannually run any command to resolve this error.
would you please let me know what should I do ?
Thanks,
Dipti Chhatrapati
Monday, April 23, 2012 6:34 AM -
try to use overridden version of EndpointAddress constructor and pass EndpointIdentity to it:
var address = new EndpointAddress(endpointElement.Address, EndpointIdentity.CreateDnsIdentity("localhost"));
(in this example dns identity is used, but you should use identity which is used for your service, e.g. upn (user principal identity)).
Blog - http://sadomovalex.blogspot.com
CAML via C# - http://camlex.codeplex.comMonday, April 23, 2012 9:54 PM