Wednesday, March 16, 2011

Reader Quotas for WCF Services in SharePoint 2010

Creating a WCF service in SharePoint 2010 is relatively easy.  The first thing to do is follow the basic steps in Microsoft's post "Creating a Custom WCF Service in SharePoint Foundation".  This will give you a nice starting point for your Service as long as you don't need to need to change any of the Binding information.

Try sending a 70k file to your nice new WCF service.  Oops  error!

The remote server returned an unexpected response: (400) Bad Request.*
Or

The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.

Ok no problem.  If you have done any WCF Service code in the past you probably know that you need to alter some of the binding information to increase the message size and or change the attributes for the readerQuotas no big deal right? 

Wrong!!! 

SharePoint 2010 has this great new hosting ability MultipleBaseAddressBasicHttpBindingServiceHostFactory and it does all the work required to set up the end points and bindings. You don't need to do create a web.config file or write any WCF service code.

So right about now you should be asking yourself "Self, how do I change the binding information then?".
Well after hunting around for the better part of a day I found a couple of post that talked about using the SPWebService.ContentService to programmatic change the binding information.
For example:


But no where do any of the post talk about where do I run this code.  Wouldn't make sense to run it on the client, although you do need to make some changes on the client side as well.  And running it as a Event Receiver when the feature for the WCF Service is installed did not work for me ether.

So what next???
Well I finally came across a comment on one of the MSDN pages by Dan Mayernik.

First, to update the configuration for a custom WCF service -add a WebApplication level feature with nothing more then a feature receiver. The receiver should override "FeatureInstalled" as follows:



Obviously, the values listed above are the extreme cases and should be set according to what your service will need. Also, the key for the indexer used for the WcfServiceSettings collection is the name of your service file. If you place the file in a subdirectory -do not include the subdirectory name as part of the key.

Thus, if your service is deployed to the ISAPI directory under:
/MyCompany/MyCustomService.svc

the key for the WcfServiceSettings collection would be:
mycustomservice.svc
 

CAUTION: The key, as stated above, is case sensitive. The key should be all lowercase.



Thanks goes to Dan he saved me from loosing any more of my hair (this week)

Monday, July 12, 2010

Reusing file in Visual Studio without copying

This is so silly but I just figured out how to do it and I had to post it (in case someone else is as brain dead as I was).
When you want to add an existing file to your current project in Visual Studio that's located outside the current project's directory, that file is copied into the project folder, then it's added to the project.

Well what if you don't want a duplicate file?

Maybe you want to share the same source file in multiple projects so that if you modify the code in one project (to fix a bug, for example) the updated code is immediately available to the other projects.

I know, I know, most of us would create some sort of shared library for this code and just reference this DLL or even the whole VS project, but to add a shared file all you need to do is instead of clicking the 'Open' button when adding an existing file, click the arrow to the left of this button. Then select 'Link File' from drop down, this way you create a link to the original file without coping it locally.
Simple!

Friday, January 15, 2010

SharePoint 2007 Custom workflow failed to start.


I am doing some custom SharePoint 2007 Workflow development utilizing WSPBuilder and I had this one workflow that just would not run no matter what I did. I disabled all the activities in it except a Workflow History logging activity but still the workflow would not start. I am a little embarrassed to say this was driving me insane for the better part of a day.

No matter what I did when I ran the workflow I would get

1/15/2010 11:06 AM Error
System Account
Trans failed to start. 1/15/2010 11:06 AM Workflow Canceled
System Account
Workflow Trans was canceled by System Account.

I searched through the SharePoint logs over and over again and all I was seeing was unable to load workflow assembly, but I knew it would load since I had other workflows in this library and they all ran no problem.

Here is the error message out of the log file, I hope you spot the problem:

Load Workflow Assembly: System.IO.FileLoadException: Could not load file or assembly 'com.XXXX.Sharepoint.Workflows\, Version\=1.0.0.0\, Culture\=neutral\, PublicKeyToken\=$PublicKeyToken$' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) File name: 'com.XXXX.Sharepoint.Workflows\, Version\=1.0.0.0\, Culture\=neutral\, PublicKeyToken\=$PublicKeyToken$' at System.Reflection.AssemblyName.nInit(Assembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent) at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) at System.Reflection.Assembly.Load(String assemblyString) at ...

Well that part I was not seeing was PublicKeyToken\=$PublicKeyToken.
It turns out that when I created the feature using the WSPBuilder create SyncSharePointWorkflow option it must have had an error I did not notice and this error caused it not to add the PublicKeyToken to the elements.xml file. Or maybe it never did and I just forgot to update this line.

CodeBesideAssembly="com.XXXX.Sharepoint.Workflows, Version=1.0.0.0, Culture=neutral, PublicKeyToken=$PublicKeyToken$"

Anyway once I added the correct Public Key, retracted and redeployed the workflow, it ran fine.

Hope this helps someone else save a day!!!

Thursday, March 12, 2009

Windows Media Center

I dont know how may of you out there are using Windows Media Center for your TV viewing needs, but I have to say http://www.secondrun.tv/ is the best add-on that I have come across yet!
I have completely dropped my Dish Network and almost exclusively use this to watch my favorite shows.

Friday, February 13, 2009

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server

Well the bad news is that this worked for some time but then started to not work again. Looks like i am back to the drawing board.


Ok it took me 2 days to track this down but I think I have solved it.

I have 2 different .net 2.0 libraries in the same application that both access the same third-party Java web service. Oh by the way this worked no problem in .net 1.0

Well this was throwing an System.Net.WebException "The underlying connection was closed:" randomly.

InnerException: System.IO.IOException
Message="Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
Source="System"
StackTrace:
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
InnerException: System.Net.Sockets.SocketException
Message="An existing connection was forcibly closed by the remote host"
Source="System"
ErrorCode=10054
NativeErrorCode=10054
StackTrace:
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)


I spent forever searching the net looking for a solution to this, with little to no success.
Well I finally came across this link. To resolve this problem you need to disable the keep-alive feature.

In the .NET Framework, you need to set the HttpWebRequest.KeepAlive property to FALSE.
To do this I created a web service reference then modified the generated Reference.cs class and added

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest webRequest = (HttpWebRequest)base.GetWebRequest(uri);
webRequest.KeepAlive = false;
return webRequest;
}

now I know this will probably add a little over-head to my application but so what it solved my devastating error :-)

Wednesday, December 10, 2008

My new HTPC

Ok I just created a new HTPC (Home Theater PC) and let me just say how the hell did I live with out this before. I actually was able to drop my Dish Network service since most of what I was watching is offered online.
When I get a chance I hope to list everything I went thru in creating it.

Tuesday, September 9, 2008

.net Exception list

Jeff Atwood wrote a great little block of code to list all the exceptions in .net. I just needed the most common ones so I posted them here.

System.AppDomainUnloadedException
System.ApplicationException
System.ArgumentException
System.ArgumentNullException
System.ArgumentOutOfRangeException
System.ArithmeticException
System.ArrayTypeMismatchException
System.BadImageFormatException
System.CannotUnloadAppDomainException
System.ComponentModel.Design.CheckoutException
System.ComponentModel.Design.Serialization.CodeDomSerializerException
System.ComponentModel.InvalidEnumArgumentException
System.ComponentModel.LicenseException
System.ComponentModel.WarningException
System.ComponentModel.Win32Exception
System.Configuration.ConfigurationException
System.ContextMarshalException
System.Data.ConstraintException
System.Data.DataException
System.Data.DBConcurrencyException
System.Data.DeletedRowInaccessibleException
System.Data.DuplicateNameException
System.Data.EvaluateException
System.Data.ExprException
System.Data.InRowChangingEventException
System.Data.InvalidConstraintException
System.Data.InvalidExpressionException
System.Data.MissingPrimaryKeyException
System.Data.NoNullAllowedException
System.Data.Odbc.OdbcException
System.Data.OleDb.OleDbException
System.Data.ReadOnlyException
System.Data.RowNotInTableException
System.Data.SqlClient._ValueException
System.Data.SqlClient.SqlException
System.Data.SqlTypes.SqlNullValueException
System.Data.SqlTypes.SqlTruncateException
System.Data.SqlTypes.SqlTypeException
System.Data.StrongTypingException
System.Data.SyntaxErrorException
System.Data.TypedDataSetGeneratorException
System.Data.VersionNotFoundException
System.DivideByZeroException
System.DllNotFoundException
System.Drawing.Printing.InvalidPrinterException
System.DuplicateWaitObjectException
System.EntryPointNotFoundException
System.Exception
System.ExecutionEngineException
System.FieldAccessException
System.FormatException
System.IndexOutOfRangeException
System.InvalidCastException
System.InvalidOperationException
System.InvalidProgramException
System.IO.DirectoryNotFoundException
System.IO.EndOfStreamException
System.IO.FileLoadException
System.IO.FileNotFoundException
System.IO.InternalBufferOverflowException
System.IO.IOException
System.IO.IsolatedStorage.IsolatedStorageException
System.IO.PathTooLongException
System.Management.ManagementException
System.MemberAccessException
System.Messaging.MessageQueueException
System.MethodAccessException
System.MissingFieldException
System.MissingMemberException
System.MissingMethodException
System.MulticastNotSupportedException
System.Net.CookieException
System.Net.ProtocolViolationException
System.Net.Sockets.SocketException
System.Net.WebException
System.NotFiniteNumberException
System.NotImplementedException
System.NotSupportedException
System.NullReferenceException
System.ObjectDisposedException
System.OutOfMemoryException
System.OverflowException
System.PlatformNotSupportedException
System.RankException
System.Reflection.AmbiguousMatchException
System.Reflection.CustomAttributeFormatException
System.Reflection.InvalidFilterCriteriaException
System.Reflection.ReflectionTypeLoadException
System.Reflection.TargetException
System.Reflection.TargetInvocationException
System.Reflection.TargetParameterCountException
System.Resources.MissingManifestResourceException
System.Runtime.InteropServices.COMException
System.Runtime.InteropServices.ExternalException
System.Runtime.InteropServices.InvalidComObjectException
System.Runtime.InteropServices.InvalidOleVariantTypeException
System.Runtime.InteropServices.MarshalDirectiveException
System.Runtime.InteropServices.SafeArrayRankMismatchException
System.Runtime.InteropServices.SafeArrayTypeMismatchException
System.Runtime.InteropServices.SEHException
System.Runtime.Remoting.MetadataServices.SUDSGeneratorException
System.Runtime.Remoting.MetadataServices.SUDSParserException
System.Runtime.Remoting.RemotingException
System.Runtime.Remoting.RemotingTimeoutException
System.Runtime.Remoting.ServerException
System.Runtime.Serialization.SerializationException
System.Security.Cryptography.CryptographicException
System.Security.Cryptography.CryptographicUnexpectedOperationException
System.Security.Policy.PolicyException
System.Security.SecurityException
System.Security.VerificationException
System.Security.XmlSyntaxException
System.ServiceProcess.TimeoutException
System.StackOverflowException
System.SystemException
System.Threading.SynchronizationLockException
System.Threading.ThreadAbortException
System.Threading.ThreadInterruptedException
System.Threading.ThreadStateException
System.Threading.ThreadStopException
System.TypeInitializationException
System.TypeLoadException
System.TypeUnloadedException
System.UnauthorizedAccessException
System.UriFormatException
System.Web.HttpApplication+CancelModuleException
System.Web.HttpCompileException
System.Web.HttpException
System.Web.HttpParseException
System.Web.HttpRequestValidationException
System.Web.HttpUnhandledException
System.Web.Services.Discovery.InvalidContentTypeException
System.Web.Services.Discovery.InvalidDocumentContentsException
System.Web.Services.Protocols.SoapException
System.Web.Services.Protocols.SoapHeaderException
System.Windows.Forms.AxHost+InvalidActiveXStateException
System.Xml.Schema.XmlSchemaException
System.Xml.XmlException
System.Xml.XPath.XPathException
System.Xml.Xsl.XsltCompileException
System.Xml.Xsl.XsltException