Monday, April 25, 2011

How to change LINQ O/R-M table name/source during runtime?

I've got a database, and an entityset created by the O/R-Mapper, using all this with LINQ.

In the O/R-Mapper I need to enter a table name (source) for every table, which is being used for the SQL generated by LINQ. In the .dbml file it looks like this:

<Table Name="dbo.Customers" Member="Customers">

Now I'd like to change this table name during runtime, so the SQL will be run against some other table (customers2008 instead of customer, for example).

Is there any way to change the table name (source name) during runtime?

[Update] After some testing to my dismay I had to discover that XmlMappingSource does render computed properties which are not persisted in the database unaccessable (yes, even the mapping created by SqlMetal does ignore everything which is not persisted).

From stackoverflow
  • Kind of ... You'd have to do something like this:

    1. Create the entities in the designer.
    2. Use SqlMetal to generate an xml mapping file from the .dbml file generate by the designer.
    3. Update the table names in the xml file.
    4. Then in your code, use the DataContext constructor overload that accepts an XmlMappingSource created with the xml mapping file.

    Here's a blog post that explains this process step by step: http://weblogs.asp.net/guybarrette/archive/2008/07/23/linq-to-sql-dynamic-mapping.aspx

    I'm using a similar process with an ERP database that has table names like ttccom001xxx where xxx is the installation ID (I know it's a horrible schema, but there's nothing I can do about it). We have multiple installations, so I copy the initial xml mapping once for each installation and then replace xxx with the installation ID. I wrote a small console app that uses Regex to take care the replacements and added it as part of my build process.

    Sam : Now I only need a way to create this mapping files during runtime, so I won't have to try and keep the mapping file in alignment with the data classes.
    Sam : Oh well, I managed to create these files, but alas, they do not work as expected. Running out of time now, more tomorrow.
    jrummell : I would recommend creating the xml mapping in the post-build event instead of at runtime.
    Sam : Even the mapping files created with SqlMetal do render the DataContext unusable, since they disable access to computed properties which are not persisted in the database. Pity.
    jrummell : Did you add these properties in a partial class? If so, they should still be accessible. You wouldn't be able to use them in a query, however, because they don't map to a column in the database. This is true for xml and attribute based mapping.
  • Here's a link to Microsoft's External Mapping Reference.

    External mapping overrides attribute-based mapping.

    Here's the essentials.

    1. Create some xml that represents your mappings.
    2. New up an XmlMappingSource and give it your xml.
    3. When you construct your DataContext, use a Constructor that accepts the XmlMappingSource, such as this one.
    Sam : You don't happen to have a link or some source how to create a mapping file in a running program?

Can an ASP web service return a ZIP file?

I'd like this to be possible!

Can you write a web service (using C# and an asmx) that returns a ZIP file?

If so, can you give a simple example?

Thanks

From stackoverflow
  • Sure thing,

    No matter if it's a ZIP, DOC, PDF. We call that MTOM in the SOAP world

    see this example yourself

  • This article shows how to return a file from webservice: http://articles.techrepublic.com.com/5100-10878_11-5805105.html.

  • I'm not sure you can use MTOM with vanilla .NET asmx services ( see here) . I know it's possible if using WCF though.

    As a (poor) alternative, you could write a web method that returns the zipped data as a base64-encoded string (which would be probably quite long depending on the size of the file, so useful for small datasets only). Then you decode the string in the client to get the original data.

    Tony Peterson : For sending the base64 encoding back to the caller, should the InsertLineBreaks option of Base64FormattingOptions be used?
    axel_c : I think you can get away with just Convert.ToBase64String (byte[] data). See http://msdn.microsoft.com/en-us/library/system.convert.tobase64string.aspx for details.
  • Thanks empi, I've got it all working. Marvellous...

Can signed applets connect with a different host from which they originate?

I need an applet to open a socket and communicate with a server listening on the local host to which the applet downloaded (the end user machine).

contrary to what I have read about applet security, it seems that even signed applets cannot open a socket to a different host from which they were downloaded (on the same machine it works perfectly)

I have certified the applet using -selfcert, signed it using jarsigner, and still, whenever it tries to open a socket to a different host, I get:

Java.lang.Exception: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:9999 connect,resolve)

I even tried changing the Java policy file, although with signed applets it is not required to do so:

grant codeBase "http://applethost:8080/socket" { permission java.security.AllPermission; permission java.lang.RuntimePermission "usePolicy"; };

What is the deal with sigend applets, can they connet to a different host or not?

From stackoverflow
  • Yes, when you load your applet, if you choose to accept its certificate and trust it, it is granted AllPermission, which includes SocketPermission. I have written a signed applet before that connects to a host other than the one from which it was loaded. You could try temporarily changing your java policy file to just have

    grant {
      permission java.security.AllPermission;
    };
    
    • Look in your policy file to see if it defines any other policy.url locations, perhaps they are interfering.
    • Check your browser settings for javascript maybe.
    • Make sure that you accepted the certificate for the applet and that it gets installed to your list of site certificates.
    • Make sure the grant codeBase line you have is the same as the codebase in your applet's manifest.
    • You could try printing out the list of permissions that your applet has before you try the connect.
    • You could try to programatically grant AllPermission from within the applet.
    adilei : @John, it seems that just granting permission java.security.AllPermission; works but that is still the only way I could make it work. I made the applet print its codeBase and it seems fine. Very frustrating, but at least thanks to you I know it is security related indeed and not some weird issue.
    adilei : @John, I am going to accept this answer since it's true - it is possible for an applet to open a socket to a different host. I am going to set up a follow up question though if you are interested in following me there. Cheers, Adi.
    A. Levy : How can you "programatically grant AllPermission"? What is the point of having a security manager to prevent execution of malicious code if the code can just grant itself permissions to do whatever it wants? It that is even possible, it shouldn't be!. @John, if you read this, could you please delete that last bullet to prevent other people from going down the wrong trail by trying to implement something the Java standard explicitly prevents. Either that or explain why I'm wrong. Thanks!
    Knife-Action-Jesus : I know this is old, but you can't do this programatically. It would defeat the whole purpose of applet security. The proper way to get the permissions you need is to sign the jar. And wrap all of your code in a privileged block.

Looking for MsPaint alternative with alpha support

In our codebase I write many things that, for testing, depends on the image Im loading have alpha. Usually, I make all my own testimages/programmers-art in MsPaint. But every time I need an image with alpha, I have to go and disturb one of our artists. (Or start the loading of GIMP and go for a coffee break.)

So Im looking for alternatives, as quick and simple as MsPaint but which has alpha support. I tried GIMP but the time it takes to load that behemoth makes it not an option.

Is there no software in the middle grounds between mspaint and gimp? From the looks of my googling and searching through wikipedia, most raster graphics editors aim to be a Photoshop alternative.

From stackoverflow
  • Paint.NET

    It's basically exactly what you're asking for. Not hardcore as Photoshop, more advanced than Paint.

    Built on the .NET Framework so may only run on Windows (might work with Mono, I don't know), but as you've said you use Paint I'm assuming you're on Windows anyway.

    Matt Hamilton : I'd upvote this twice if I could. :)
    banjollity : +1 for Paint.NET (and if I were 38 seconds quicker those upvotes would be mine!!! :)
    Garry Shutler : Hehe, yes, just a little too slow :)
    banjollity : Enjoy your reputation Mr Shutler. I'll get you next time. *shakes fist*
    WestDiscGolf : +1 fast finger typing :-)
  • Paint.Net is a simple and easy alternative that gives you a little more features than MSPaint, without having to load big heavy monsters (or read 500-page manuals).

    As far as I can tell, it supports Alpha quite nicely, as well as many other features a non-artist might need for their graphics work.

    http://www.paint.net/

    mizipzor : Seems like Paint.Net is the way to go. Found it on wikipedia but deemed to much photoshop-ish. Im downloading it now, and to compensate for your "lost" upvotes, Ill accept your answer. ;)

getting System.ServiceModel.AddressAccessDeniedException on Vista for WCF service

We have an application that starts a WCF server and the app reads from it.

It starts and can be read fine on all Windows XP machines.

On our Vista machine, however, we get:

System.ServiceModel.AddressAccessDeniedException
"The process has no rights to this namespace."
System.Net.HttpListenerException

The URL is at localhost:

http://localhost:8731/ABC.Testing.Business.Core/SecurityService/?wsdl

The error message sent us here:

http://msdn.microsoft.com/en-us/library/ms733768.aspx

And we did what it said:

netsh http add urlacl url=... user=...

which changed the rights but the WCF service still cannot start on Vista.

Does anyone have any experience with this that could move us forward? Thanks.

From stackoverflow
  • I've had similar problems on Vista, which I haven't figured out yet. What error is it throwing now that you have run that netsh command? Have you tried giving that user local administrator privileges?

    Edward Tanguay : In the netsh command I gave the rights to the user than runs the application, but what I think I need to do is find out which process needs the rights, but this is a Unity-based app and the WCF is started before it even gets to the bootstrapper, so which process do I need to give rights to?
    Andy White : I think I'm in the same boat as you now... not sure what to do :) I've done the same thing with my service (netsh command), and it still won't run in Vista, but it runs fine in XP if I run the equivalent httpcfg command. There must be some cryptic setting somewhere in Vista that is block us.
  • Hi Edward, try reading through this blog post and see if it helps.

    http://blogs.msdn.com/paulwh/archive/2007/05/04/addressaccessdeniedexception-http-could-not-register-url-http-8080.aspx

How to check that a disk is RAID1 on Windows Server 2003/2008?

I am usually renting dedicated servers under Windows Server 2003/2008. Those servers are said to be "hardware RAID1"; yet I am wondering how can I verify this property of the server remotely?

As far I can see, disks appears as classical IDE disk from the windows disk manager, and it does not tell me anything about the RAID properties of the disk.

From stackoverflow
  • Probably, you can't. Hence the "hardware"-ness of the setup – the drives appear to the operating system as a single IDE drive.

  • You'd need to install the dedicated vendor disk management tools. eg Dell used to replace the disk manager MMC snap in with one that showed all this stuff.

    Windows itself does not care, whether it's a 10 disk RAID 10 or a single one.

    Joannes Vermorel : Thank for the answer. Hum, what if you have not single clue about the identity of the vendor? :-)
    gbn : Ask your vendor? For one of my projects, we can see what the server make and model is when we rent it...

How to get Eclipse to give Cygwin's GDB a posix path to the executable?

I'm using Eclipse on Windows to program C++ using Cygwin's toolchain. I would like to use it for debugging as well, but my problem is the following:

  • The executable to debug is in a sub-folder of the workspace (pretty standard, I guess) but not of the actual Project from which the executable is built.
  • Eclipse and GDB have different path formats: Eclipse works with Windows paths, whereas GDB works with posix paths. Eclipse knows D:\Path\To\Exe whereas GDB needs /cygdrive/d/Path/To/Exe.
  • Eclipse insists that it can find the executable to debug when I enter the path in the "Debug configurations..." dialog. The "Debug" button is disabled until the path is correct.
  • Eclipse does not seem to take ${workspace_loc} in that particular input box.

Is there some hidden option I don't know about? Have you had the problem before?

What I've tried so far

  • Forcing Eclipse to pass the path I input to GDB --- did not work.
  • Fiddling with source lookup paths and path mappings --- did not work, it's really just for source files.
  • Creating a C++ Project for the output folder where the executable is, so I could have a relative path to the exe inside the project --- did not work.

Things I could still try

  • Use the .gdbinit file to discard the path given by Eclipse and load the real executable with posix paths --- tried that, but not very hard.
  • Specify a wrapper as argument to GDB --- pollutes the root of my workspace.
  • Provide my own plug-in to deal with the case --- adds another piece of code to maintain in another language.
From stackoverflow
  • Cygwin comes with a program called cygpath to translate paths from windows to unix and vice-versa, e.g.

    $ cygpath -u 'c:\Users\Andrew\Documents\svn'
    /cygdrive/c/Users/Andrew/Documents/svn
    

    do a cygpath --help for more info.

    So you probably want to write a wrapper for GDB that translates any windows-specific paths to unix paths, and then invokes the real GDB.

  • In Eclipse in Preferences->C/C++->Debug->"Common Source Lookup Path" add new path mapping. As compilation path you should use Unix like path (e.g. /cygdrive/c), as local system path you should use Windows like path (e.g. c:).

    Carl Seleborg : Thanks, pensamiento, I did try that, but it did not work. My understanding is that this path mapping does not affect the path that Eclipse hands to GDB as executable to debug. The name of the setting does not suggest that this should work, either.
    Ben Gartner : This worked for me. Thanks pensamiento!
  • The problem went away when upgrading to Eclipse Ganymede.