Wednesday, August 7, 2013

‘MyEntities’: type used in a using statement must be implicitly convertible to ‘System.IDisposable’

‘MyEntities’: type used in a using statement must be implicitly convertible to ‘System.IDisposable’

When trying to access the entity object which is in a different project, I added the reference of the project having the entity object to the new project and was trying to use the code block as below:

 using (Entities entities = new Entities(entityConnStr))
{

}

This gave me the error. The solution for this was to add a reference to 'System.Data.Entity' in the new project. After this it started working for me.


Third party WCF Web Service files (if your dev machine doesn't have access to the url of the web service) - getting WSDL - SVCUtil.exe

Run SVCUtil.exe from Visual Studio command prompt, with the url of the third party web service from a computer where you have access to the third party web service.


It will generate a .CS and .CONFIG file. The .CS file has all the method, parameter and return value information. The .CONFIG file has all the end point details.

Add the .CS file to the client project and add the details from the .CONFIG to the client config file.

Now you can access the third party web service (if your dev machine doesn't have access to the url of the web service). This is just a tip.

Creating Custom Properties for/extending the Entity framework generated class

How to create a custom property for an entity framework generated class. I had a requirement and the way I did was to create a partial class for the entity generated class. After creating the partial class, I added a property and was able to access the property in the same project.

BUT here comes problem.... This entity generated class was in a web service. After I deployed my web service and tried to test with a client application, I was not able to access the newly added custom property.


public partial class ClassA                       //original class, entity generated
    {
        public string Name { get; set; }
      
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String FROM_USER
        {
            get
            {
                return _FROM_USER;
            }
            set
            {
                OnFROM_USERChanging(value);
                ReportPropertyChanging("FROM_USER");
                _FROM_USER = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("FROM_USER");
                OnFROM_USERChanged();
            }
        }
        private global::System.String _FROM_USER;
        partial void OnFROM_USERChanging(global::System.String value);
        partial void OnFROM_USERChanged();
    }

public partial class ClassA                      //newly created partial class for the above class
    {
        public int ID { get; set; }
    }

 The above partial class didn't work for me. So I had to add an extra attribute for the new property as below:

public partial class ClassA
    {
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public int ID { get; set; }
    }

 This solution worked for me. Hope this tip helps you.












ERROR - Keyset does not exist - When a web site hosted in IIS, have a certificate installed in server.


[FaultException`1: Keyset does not exist
]
   System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +9442991
   System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +345


I was accessing a WCF web service from a web application hosted in IIS. The call to the external web service is validated with a certificate installed in server. This certificate should match with the certificate in the third party side. After successful validation, we get the output.

But I was getting error as mentioned above. It was something related to the certificate. Finally I found out that the account with which my web application runs doesn't have access to the certificate. So I had to add the user account  "Network Service" full rights to the certificate. It is done as below:

1. Type mmc(Microsoft Management Console) from run.
2. Click on File --> Add/Remove Snaps-ins
3. Select Certificates from Available snap-ins and Add to Selected snap-ins.
4. Use Computer account from next prompt.
5. Click Next and select Local Computer and click Finish, now the certificate is added in selected snap-ins list, click OK.
6. Click certificates and find your certificate and right click --> All tasks --> Manage Private Keys
7. Add "NETWORK SERVICE" account with Full control. Click OK.

After this you will be able to access the third party web service from your web app hosted in IIS.

Basically the account with which your web app runs needs to have access to the certificate. This worked for me like a charm.

Hope this helps somebody without wasting much time trying to figure out, where does the error come from.